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

本文详细介绍了Python的tabulate库,它用于将数据以表格形式美观地展示。通过示例展示了不同表格样式,如plain、simple、grid、fancy_grid等,并提到了针对中文对齐的问题,以及如何利用wcwidth模块解决。此外,还涵盖了tabulate支持的各种输出格式,如html、latex等。
摘要由CSDN通过智能技术生成

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写于上海

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值