当前位置: 首页 > 工具软件 > tabulate > 使用案例 >

python tabulate_Python对齐显示||tabulate函数||wcwidth模块||知道这些就够了

孔阳炎
2023-12-01

tabulate函数官方文档

tabulate直译是制表,让python实现表格化显示。

tabulate.tabulate(tabular_data, headers=(), tablefmt=u'simple', floatfmt=u'g', numalign=u'decimal', stralign=u'left', missingval=u'')

tabular_data:需要显示的内容。

headers:表头

tablefmt:显示格式,共有如下各种:

- "plain"

- "simple"

- "github"

- "grid"

- "fancy_grid"

- "pipe"

- "orgtbl"

- "jira"

- "presto"

- "psql"

- "rst"

- "mediawiki"

- "moinmoin"

- "youtrack"

- "html"

- "latex"

- "latex_raw"

- "latex_booktabs"

- "textile"

常用函数

tabulate是一个模块,其中包括tabulate.tabulate()函数

from tabulate import tabulate

tabulate.PRESERVE_WHITESPACE = True # 保留空格

中文对齐||wcwidth模块

tabulate默认没有考虑中文字符宽度,因此无法对齐,如要实现对齐,需要wcwidth 包。

输出表格样式

tabulate 提供多种表格输出风格,示例如下:

plain

>>> print(tabulate(table_data, headers=table_header, tablefmt='plain'))

Name Chinese Math English

Tom 90 80 85

Jim 70 90 80

Lucy 90 70 90

simple

>>> print(tabulate(table_data, headers=table_header, tablefmt='simple'))

Name Chinese Math English

------ --------- ------ ---------

Tom 90 80 85

Jim 70 90 80

Lucy 90 70 90

grid

>>> print(tabulate(table_data, headers=table_header, tablefmt='grid'))

+--------+-----------+--------+-----------+

| Name | Chinese | Math | English |

+========+===========+========+===========+

| Tom | 90 | 80 | 85 |

+--------+-----------+--------+-----------+

| Jim | 70 | 90 | 80 |

+--------+-----------+--------+-----------+

| Lucy | 90 | 70 | 90 |

+--------+-----------+--------+-----------+

fancy_grid

>>> print(tabulate(table_data, headers=table_header, tablefmt='fancy_grid'))

╒════════╤═══════════╤════════╤═══════════╕

│ Name │ Chinese │ Math │ English │

╞════════╪═══════════╪════════╪═══════════╡

│ Tom │ 90 │ 80 │ 85 │

├────────┼───────────┼────────┼───────────┤

│ Jim │ 70 │ 90 │ 80 │

├────────┼───────────┼────────┼───────────┤

│ Lucy │ 90 │ 70 │ 90 │

╘════════╧═══════════╧════════╧═══════════╛

pipe

>>> print(tabulate(table_data, headers=table_header, tablefmt='pipe'))

| Name | Chinese | Math | English |

|:-------|----------:|-------:|----------:|

| Tom | 90 | 80 | 85 |

| Jim | 70 | 90 | 80 |

| Lucy | 90 | 70 | 90 |

orgtlb

>>> print(tabulate(table_data, headers=table_header, tablefmt='orgtbl'))

| Name | Chinese | Math | English |

|--------+-----------+--------+-----------|

| Tom | 90 | 80 | 85 |

| Jim | 70 | 90 | 80 |

| Lucy | 90 | 70 | 90 |

jira

>>> print(tabulate(table_data, headers=table_header, tablefmt='jira'))

|| Name || Chinese || Math || English ||

| Tom | 90 | 80 | 85 |

| Jim | 70 | 90 | 80 |

| Lucy | 90 | 70 | 90 |

presto

>>> print(tabulate(table_data, headers=table_header, tablefmt='presto'))

Name | Chinese | Math | English

--------+-----------+--------+-----------

Tom | 90 | 80 | 85

Jim | 70 | 90 | 80

Lucy | 90 | 70 | 90

psql

>>> print(tabulate(table_data, headers=table_header, tablefmt='psql'))

+--------+-----------+--------+-----------+

| Name | Chinese | Math | English |

|--------+-----------+--------+-----------|

| Tom | 90 | 80 | 85 |

| Jim | 70 | 90 | 80 |

| Lucy | 90 | 70 | 90 |

+--------+-----------+--------+-----------+

rst

>>> print(tabulate(table_data, headers=table_header, tablefmt='rst'))

====== ========= ====== =========

Name Chinese Math English

====== ========= ====== =========

Tom 90 80 85

Jim 70 90 80

Lucy 90 70 90

====== ========= ====== =========

html

>>> print(tabulate(table_data, headers=table_header, tablefmt='html'))

Name Chinese Math English

Tom 90 80 85Jim 70 90 80Lucy 90 70 90

注意到, tabulate 函数也可以用来生成 html 表格定义代码。 此外,还支持 mediawiki、 moinmoin 、 youtrack 、 latex 、 latex_raw 、 latex__booktabs 、 textile 表格生成。

参考

.

.

.

2019-03-25 13:11:00写于上海

 类似资料: