我正在使用 PrettyTable 以漂亮的表格格式将数据打印到终端。按单列排序很容易打印。
from prettytable import PrettyTable
table = PrettyTable(["Name", "Grade"])
table.add_row(["Joe", 90])
table.add_row(["Sally", 100])
print table.get_string(sortby="Grade", reversesort=True)
>> Table with Sally on top, because her score is highest.
我的麻烦是我想对两列进行排序。在这个代理案例中,我想按年级打印,如果有平局,则按字母顺序打印。
table = PrettyTable(["Name", "Grade"])
table.add_row(["Joe", 90])
table.add_row(["Sally", 100])
table.add_row(["Bill", 90])
print table.get_string(sortby=("Grade","Name"), reversesort=True)
>> Doesn't work
文档说 sort_key 将允许我编写一个函数来完成此操作,但我还没有看到一个实际的实现来工作。