tabulate

优雅打印表格数据

pip install tabulate

  • 支持二维列表、二维迭代类型、字典迭代等类型
  • 支持NumPy二维数组、pandas.DataFrame类型
  • 输出表格可以通过参数自定义风格

简单小实例

1
2
3
4
5
6
7
8
9
10
11
from tabulate import tabulate
ls = [['北京',100,98.2],['上海',98,91.3],['广州',94,61.2]]
print(tabulate(ls))

'''
---- --- ----
北京 100 98.2
上海 98 91.3
广州 94 61.2
---- --- ----
'''

tabulate函数

<function tabulate.tabulate(tabular_data, headers=(), tablefmt=’simple’, floatfmt=’g’, numalign=’decimal’, stralign=’left’, missingval=’’, showindex=’default’, disable_numparse=False, colalign=None)>

  • tabular_data:表格数据
  • headers:表头,元组或列表
  • tablefmt:表格风格
    • plain
    • simple
    • grid
    • fancy_grid
    • pipe
    • orgtbl
    • jira
    • presto
    • psql
    • rst
    • mediawiki
    • moinmoin
    • youtrack
    • html
    • latex
    • latex_raw
    • latex_booktabs
    • textile
  • floatfmt
  • numalign:全表格数字的对齐方式,默认小数点对齐