簡體   English   中英

以表格格式漂亮地打印列表

[英]Pretty printing a list in a tabular format

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

我的清單采用以下格式。

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

我試過pprint ,但它不會以表格格式打印結果。

編輯:我想查看以下格式的 output:


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 左對齊。

你會在這里找到精度

但是指定一個恆定的長度來在字符串的末尾打印一些東西是沒有意義的,因為在這種情況下它有點自然左對齊。 然后:

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()會更容易進行這種格式化

也許是這樣的:

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

從您的示例 output 看起來,“表格”可能意味着選項卡。

如果這是正確的,這似乎工作:

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

對於任意項目數和任意行數,您可以使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM