开发者

Pretty printing a list in a tabular format

Using Python 2.4, how do I print a list in a nice tabular format?

My list is in the below format.

mylist=[(('VAL1', 'VAL2', 'VAL3', 'VAL4', 'VAL5', 'VAL6'), AGGREGATE_VALUE)]

I have tried pprint, but it does not print the result in a tabular format.

EDIT : I would like to see the output in the below format:


VAL1        VAL2     VAL3    VAL4  &n开发者_如何学Cbsp; VAL5    VAL6        AGGREGATE_VALUE


This table, should account for variable item lengths and still print with proper indentation.


mylist = [ ( ('12', '47', '4', '574862', '58', '7856'), 'AGGREGATE_VALUE1'),
           ( ('2', '75', '757', '8233', '838', '47775272785'), 'AGGREG2'),
           ( ('4144', '78', '78965', '778', '78578', '2'), 'AGGREGATE_VALUE3')]

longg = dict.fromkeys((0,1,2,3,4,5,6),0)

for tu,x in mylist:
    for i,el in enumerate(tu):
        longg[i] = max(longg[i],len(str(el)))
    longg[6] = max(longg[6],len(str(x)))

fofo = '  '.join('%'+str(longg[i])+'s' for i in xrange(0,7))
print '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist)

result

  12  47      4  574862     58         7856  AGGREGATE_VALUE1
   2  75    757    8233    838  47775272785           AGGREG2
4144  78  78965     778  78578            2  AGGREGATE_VALUE3

Don't know if this fills your need

EDIT 1

Using string formatting with modulo operator (%) to print in a constant length, '%6s' right-justifies in a constant length of 6, and '%-6s' left-justifies in a constant length of 6.

You'll find precisions here

But there is no sense to specify a constant length to print something at the end of a string, because it's somewhat naturally-left-justified in this case. Then :

longg = dict.fromkeys((0,1,2,3,4,5,),0)

for tu,x in mylist:
    for i,el in enumerate(tu):
        longg[i] = max(longg[i],len(str(el)))

fofo = '  '.join('%'+str(longg[i])+'s' for i in xrange(0,6)) + '  %s'
print '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist)

EDIT 2

mylist = [ ( (12, 47, 4, 574862, 58, 7856), 'AGGREGATE_VALUE1'),
           ( (2, 75, 757, 8233, 838, 47775272785), 'AGGREG2'),
           ( (4144, 78, 78965, 778, 78578, 2), 'AGGREGATE_VALUE3')]

longg = dict.fromkeys((0,1,2,3,4,5),0)

for tu,_ in mylist:
    longg.update(( i, max(longg[i],len(str(el))) ) for i,el in enumerate(tu))

fofo = '  '.join('%%%ss' % longg[i] for i in xrange(0,6)) + '  %s'
print '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist)

EDIT 3

mylist = [ ( (12, 47, 4, 574862, 58, 7856), 'AGGREGATE_VALUE1'),
           ( (2, 75, 757, 8233, 838, 47775272785), 'AGGREG2'),
           ( (4144, 78, 78965, 778, 78578, 2), 'AGGREGATE_VALUE3')]

header = ('Price1','Price2','reference','XYD','code','resp','AGGREG values')

longg = dict(zip((0,1,2,3,4,5,6),(len(str(x)) for x in header)))

for tu,x in mylist:
    longg.update(( i, max(longg[i],len(str(el))) ) for i,el in enumerate(tu))
    longg[6] = max(longg[6],len(str(x)))
fofo = ' | '.join('%%-%ss' % longg[i] for i in xrange(0,7))

print '\n'.join((fofo % header,
                 '-|-'.join( longg[i]*'-' for i in xrange(7)),
                 '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist)))

result

Price1 | Price2 | reference | XYD    | code  | resp        | AGGREG values   
-------|--------|-----------|--------|-------|-------------|-----------------
12     | 47     | 4         | 574862 | 58    | 7856        | AGGREGATE_VALUE1
2      | 75     | 757       | 8233   | 838   | 47775272785 | AGGREG2         
4144   | 78     | 78965     | 778    | 78578 | 2           | AGGREGATE_VALUE3

Note that this kind of formatting would be much easier with the string's method format() introduced in Python 2.6


Try the texttable module.

Docs: http://foutaise.org/code/texttable/

PyPi: https://pypi.python.org/pypi?name=texttable&:action=display


Maybe something like this:

def tabprint(inp):
    for list_el in mylist:
        st = ''
        for word in list_el[0]:
            st += word + '\t' 
        st += str(list_el[1])

    print st


From your sample output it looks like by "tabular" you might mean tabbed.

If that's correct, this seems work:

mylist=[(('VAL1', 'VAL2', 'VAL3', 'VAL4', 'VAL5', 'VAL6'), 'AGGREGATE_VALUE')]

def flatten(arg):
    if not hasattr(arg, '__iter__'):
        yield arg
    else:
        for i in arg:
            for j in flatten(i):
                yield j

print '\t'.join(flatten(mylist))

Which outputs:

VAL1    VAL2    VAL3    VAL4    VAL5    VAL6    AGGREGATE_VALUE


For arbitrary item count and arbitrary line count you can use:

print "\n".join (map (lambda (x, y): "%s\t%s" % ("\t".join (x), y), mylist) )

For example for input

mylist = [ ( ('VAL11', 'VAL12', 'VAL13', 'VAL14', 'VAL15', 'VAL16'), 'AGGREGATE_VALUE1'),
     ( ('VAL21', 'VAL22', 'VAL23', 'VAL24', 'VAL25', 'VAL26'), 'AGGREGATE_VALUE2'),
     ( ('VAL31', 'VAL32', 'VAL33', 'VAL34', 'VAL35', 'VAL36'), 'AGGREGATE_VALUE3'),]

it yields:

VAL11   VAL12   VAL13   VAL14   VAL15   VAL16   AGGREGATE_VALUE1
VAL21   VAL22   VAL23   VAL24   VAL25   VAL26   AGGREGATE_VALUE2
VAL31   VAL32   VAL33   VAL34   VAL35   VAL36   AGGREGATE_VALUE3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜