我有一个dict
包含字符串列表,并希望将所有内容打印为终端中的表格,格式如下:
+----------------------------------------------+
| key1 | key2 | key3 | key4 |
+----------------------------------------------+
| val_1 | val_1 | val_1 | val_1 |
|----------|----------|-----------|------------|
| val_2 | val_2 | val_2 | val_2 |
+----------------------------------------------+
等
是否有一个惊人的模块或简单的方法来实现这一目标?我有一个列宽,我通过找到每个列表中最长的val来获得。
答案 0 :(得分:8)
您可以使用PrettyTable
。
apt-get install python-prettytable
安装在Ubuntu中。
#! /usr/bin/env python
from prettytable import PrettyTable
d1 = {
"key1":["val1_1", "val1_2"],
"key2":["val2_1", "val2_2"],
"key3":["val3_1", "val3_2"],
"key4":["val4_1", "val4_2"],
}
table = PrettyTable()
for key,val in sorted(d1.iteritems()):
table.add_column(key, sorted(val))
print table
结果是:
$ ./t
+--------+--------+--------+--------+
| key1 | key2 | key3 | key4 |
+--------+--------+--------+--------+
| val1_1 | val2_1 | val3_1 | val4_1 |
| val1_2 | val2_2 | val3_2 | val4_2 |
+--------+--------+--------+--------+
PrettyTable还提供HTML格式。将print table
替换为:
print table.get_html_string(attributes={"size":"100%", "class":"MyTable"})
你得到:
<table border="1" class="MyTable" size="100%">
<tr>
<th>key1</th>
<th>key2</th>
<th>key3</th>
<th>key4</th>
</tr>
<tr>
<td>val1_1</td>
<td>val2_1</td>
<td>val3_1</td>
<td>val4_1</td>
</tr>
<tr>
<td>val1_2</td>
<td>val2_2</td>
<td>val3_2</td>
<td>val4_2</td>
</tr>
</table>
答案 1 :(得分:4)
TextTable可以工作,可以通过
安装pip install texttable
http://foutaise.org/code/texttable/
https://pypi.python.org/pypi?name=texttable&:action=display
地图中的键未排序,因此无法保证特定订单。
TextTable只接受行主表排序,所以你必须对迭代器有点兴趣:
import texttable
import itertools
theDict = {"key1": ["val_1", "val_2"],
"key2": ["val_1", "val_2"],
"key3": ["val_1", "val_2"],
"key4": ["val_1", "val_2"]}
theTable = texttable.Texttable()
theIter = itertools.chain(
iter([theDict.keys()]),
itertools.imap(lambda *x: list(x), *theDict.itervalues())
)
theTable.add_rows(theIter)
print theTable.draw()
给出了:
+-------+-------+-------+-------+
| key3 | key2 | key1 | key4 |
+=======+=======+=======+=======+
| val_1 | val_1 | val_1 | val_1 |
+-------+-------+-------+-------+
| val_2 | val_2 | val_2 | val_2 |
+-------+-------+-------+-------+
答案 2 :(得分:1)
key1,key2,key3等的问题在于密钥中的dicts没有被排序。因此,我觉得可以自由地将你的dict变成一个键值元组列表,以便对键进行排序:
然后你可以这样做:
d = [ ('Coco', ['verde', 'redondo'] ),
('Plátano', ['amarillo', 'doblado'] ),
('Fresa', ['rojo', 'redondo chiquito'] ) ]
def makeTable (d):
widths = [2 + max ( [len (k) ] + [len (v) for v in vs] ) for k, vs in d]
formats = ['{:^%s}' % width for width in widths]
values = zip (* ( [k] + vs for k, vs in d) )
sep = '\n+' + '-' * (sum (widths) + len (d) - 1) + '+\n'
rows = sep.join ('|{}|'.format ('|'.join (formats [i].format (k) for i, k in enumerate (vs) ) ) for vs in values)
return sep + rows + sep
print (makeTable (d) )
要将dict转换为元组列表,只需使用[ (k, v) for k, v in d.items () ]
。
将这两件事放在一起会导致:
d = {'Coco': ['verde', 'redondo'],
'Plátano': ['amarillo', 'doblado'],
'Fresa': ['rojo', 'redondo chiquito'] }
print (makeTable ( [ (k, v) for k, v in d.items () ] ) )
答案 3 :(得分:0)
如果您只需要以格式良好的方式查看字典的内容,您可以尝试Python dict formatter and viewer,这似乎正是为了它 - 它可以显示字典(或,一个dicts列表)作为一个表或一个树。