如何使用制表模块按字母顺序对列进行分组?
How to group columns alphabetically with tabulate module?
我正在使用 tabulate
模块在控制台上很好地打印信息。我正在使用 python 2.6
我目前有这个:
+-------------------------------+
| Task | Status | Rating |
|---------+---------------------+
| A | Done | Good |
| B | Done | Bad |
| C | Pending | |
| D | Done | Good |
+---------+----------+----------+
我想去这个:
+-------------------------------+
| Task | Status | Rating |
|---------+---------------------+
| A | Done | Good |
| B | Done | Bad |
| D | Done | Good |
| C | Pending | |
+---------+----------+----------+
因此所有 Done
都组合在一起。
目前表格收到一个字典,我解压值是这样的:
def generate_table(data):
table = []
headers = ['Task', 'Status', 'Rating']
for key, value in data.iteritems():
print key, value
if 'Rating' in value:
m, l = value['Status'], value['Rating']
m = m.split('/')[-1]
temp = [key,m,l]
table.append(temp)
else:
m, l = value['Status'], None
m = m.split('/')[-1]
temp = [key,m,l]
table.append(temp)
print tabulate(table, headers, tablefmt="psql")
您可以在 for 循环后按状态列对结果 table 进行排序:
sorted(table, key=lambda status: status[1])
这将有效地 "group" 按字母顺序排列值。
我正在使用 tabulate
模块在控制台上很好地打印信息。我正在使用 python 2.6
我目前有这个:
+-------------------------------+
| Task | Status | Rating |
|---------+---------------------+
| A | Done | Good |
| B | Done | Bad |
| C | Pending | |
| D | Done | Good |
+---------+----------+----------+
我想去这个:
+-------------------------------+
| Task | Status | Rating |
|---------+---------------------+
| A | Done | Good |
| B | Done | Bad |
| D | Done | Good |
| C | Pending | |
+---------+----------+----------+
因此所有 Done
都组合在一起。
目前表格收到一个字典,我解压值是这样的:
def generate_table(data):
table = []
headers = ['Task', 'Status', 'Rating']
for key, value in data.iteritems():
print key, value
if 'Rating' in value:
m, l = value['Status'], value['Rating']
m = m.split('/')[-1]
temp = [key,m,l]
table.append(temp)
else:
m, l = value['Status'], None
m = m.split('/')[-1]
temp = [key,m,l]
table.append(temp)
print tabulate(table, headers, tablefmt="psql")
您可以在 for 循环后按状态列对结果 table 进行排序:
sorted(table, key=lambda status: status[1])
这将有效地 "group" 按字母顺序排列值。