根据
docs
你可以:
>>> myTable = PrettyTable()
>>> myTable.hrules, myTable.vrules = NONE, NONE
>>> myTable.title = 'Big Bang Theory' # this won't show, see below
>>> myTable.field_names = ["Student Name", "Class", "Section", "Percentage"]
>>> myTable.add_row(["Leanord", "X", "B", "91.2 %"])
>>> myTable.add_row(["Penny", "X", "C", "63.5 %"])
>>> myTable.add_row(["Howard", "X", "A", "90.23 %"])
>>> myTable.add_row(["Bernadette", "X", "D", "92.7 %"])
>>> myTable.add_row(["Sheldon", "X", "A", "98.2 %"])
>>> myTable.add_row(["Raj", "X", "B", "88.1 %"])
>>> myTable.add_row(["Amy", "X", "B", "95.0 %"])
>>> print(myTable.get_string(sort_key=operator.itemgetter(1, 3), sortby="Section"))
Student Name Class Section Percentage
Amy X B 95.0 %
Bernadette X D 92.7 %
Howard X A 90.23 %
Leanord X B 91.2 %
Penny X C 63.5 %
Raj X B 88.1 %
Sheldon X A 98.2 %
上面有一个问题。也就是说,它将
不
显示标题。要显示标题,您需要安装
PTable
。完成上述操作后,上面的代码将生成:
Big Bang Theory
Student Name Class Section Percentage
Amy X B 95.0 %
Bernadette X D 92.7 %
Howard X A 90.23 %
Leanord X B 91.2 %
Penny X C 63.5 %
Raj X B 88.1 %
Sheldon X A 98.2 %