pip3 install prettytable
from prettytable import PrettyTable
from prettytable import from_json, from_csv
from logzero import logger
# 表头
tb = PrettyTable()
tb.field_names = ["姓名", "年龄", "出处"]
# 添加行
tb.add_row(["zaygee", 26, "shenzhen"])
tb.add_row(["zaygdddddee2", 3, "shenzhen1"])
tb.add_row(["zayg23434ee3", 24, "shenzhen"])
tb.add_row(["zay", 45, "shenzhen2"])
# 添加多行,参数为list
list_str = [("add_row2", 26, "shenzhen"), ("add_row3", 77, "shenzhen")]
tb.add_rows(list_str)
# 添加列
# tb.add_column("性别", ["女"] * 4)
# 按某个字段倒叙排序输出
print(tb.get_string(sortby="年龄", reversesort=True))
"""
+--------------+------+-----------+
| 姓名 | 年龄 | 出处 |
+--------------+------+-----------+
| add_row3 | 77 | shenzhen |
| zay | 45 | shenzhen2 |
| zaygee | 26 | shenzhen |
| add_row2 | 26 | shenzhen |
| zayg23434ee3 | 24 | shenzhen |
| zaygdddddee2 | 3 | shenzhen1 |
+--------------+------+-----------+
"""