python – 以表格格式打印列表

使用Python 2.4,如何以漂亮的表格格式打印列表?

我的清单是以下格式.

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

我试过pprint,但它没有以表格格式打印结果.

编辑:我想看到以下格式的输出:

VAL1 VAL2 VAL3 VAL4 VAL5 VAL6 AGGREGATE_VALUE

此表应考虑可变项目长度,并仍然使用适当的缩进打印.

解决方法:

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)

结果

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

不知道这是否满足您的需求

编辑1

使用带有模运算符(%)的字符串格式以恒定长度打印,’%6s’以6的常量长度右对齐,’%-6s’左对齐,长度恒定为6.

你会发现精确度here

但是没有必要指定一个常量长度来在字符串的末尾打印一些东西,因为在这种情况下它有点自然左对齐.
然后 :

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)

编辑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)

编辑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)))

结果

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

请注意,使用Python 2.6中引入的字符串方法format(),这种格式化会更容易

上一篇:如何检查元组是否包含Python中的元素?


下一篇:python – 为什么解压缩元组会导致语法错误?