使用Python 2.4,如何以漂亮的表格格式打印列表?
我的列表格式如下。
mylist=[(('VAL1', 'VAL2', 'VAL3', 'VAL4', 'VAL5', 'VAL6'), AGGREGATE_VALUE)]
我尝试了pprint
,但它没有以表格格式打印结果。
编辑:我希望看到以下格式的输出:
VAL1 VAL2 VAL3 VAL4 VAL5 VAL6 AGGREGATE_VALUE
此表应考虑可变项目长度,并仍然使用适当的缩进打印。
答案 0 :(得分:8)
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
不知道这是否满足您的需求
使用带模数运算符(%)的字符串格式以恒定长度打印,'%6s'以6的常量长度右对齐,'% - 6s'< / strong>左对齐,长度为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)
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)
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(),这种格式化会更容易。
答案 1 :(得分:5)
尝试使用texttable模块。
文档:http://foutaise.org/code/texttable/
PyPi:https://pypi.python.org/pypi?name=texttable&:action=display
答案 2 :(得分:1)
也许是这样的:
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
答案 3 :(得分:0)
从您的示例输出看起来像“表格”,您可能意味着标签。
如果这是正确的,这似乎有效:
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))
哪个输出:
VAL1 VAL2 VAL3 VAL4 VAL5 VAL6 AGGREGATE_VALUE
答案 4 :(得分:0)
对于任意项目计数和任意行数,您可以使用:
print "\n".join (map (lambda (x, y): "%s\t%s" % ("\t".join (x), y), mylist) )
例如输入
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'),]
它产生:
VAL11 VAL12 VAL13 VAL14 VAL15 VAL16 AGGREGATE_VALUE1
VAL21 VAL22 VAL23 VAL24 VAL25 VAL26 AGGREGATE_VALUE2
VAL31 VAL32 VAL33 VAL34 VAL35 VAL36 AGGREGATE_VALUE3