-
tabulate function Official documents
tabulate literal translation is to make tables and let python realize tabular display.
tabulate.tabulate(tabular_data, headers=(), tablefmt=u'simple', floatfmt=u'g', numalign=u'decimal', stralign=u'left', missingval=u'')
tabular_data: the content to be displayed.
Headers: headers
tablefmt: display format, including the following:
- "plain" - "simple" - "github" - "grid" - "fancy_grid" - "pipe" - "orgtbl" - "jira" - "presto" - "psql" - "rst" - "mediawiki" - "moinmoin" - "youtrack" - "html" - "latex" - "latex_raw" - "latex_booktabs" - "textile"
Tabulate is a module that includes the tabulate.tabulate() function
from tabulate import tabulate tabulate.PRESERVE_WHITESPACE = True # Reserved spaces
Chinese alignment ๐ wcwidth module
Official document of wcwidth module
The Chinese character width is not taken into account by default, so it cannot be aligned. To achieve alignment, wcwidth package is required.
tabulate provides a variety of table output styles, such as:
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')) <table> <thead> <tr><th>Name </th><th style="text-align: right;"> Chinese</th><th style="text-align: right;"> Math</th><th style="text-align: right;"> English</th></tr> </thead> <tbody> <tr><td>Tom </td><td style="text-align: right;"> 90</td><td style="text-align: right;"> 80</td><td style="text-align: right;"> 85</td></tr> <tr><td>Jim </td><td style="text-align: right;"> 70</td><td style="text-align: right;"> 90</td><td style="text-align: right;"> 80</td></tr> <tr><td>Lucy </td><td style="text-align: right;"> 90</td><td style="text-align: right;"> 70</td><td style="text-align: right;"> 90</td></tr> </tbody> </table>
Note that the tabulate function can also be used to generate html table definition code. In addition, mediawiki, moinmoin, youtrack, latex, latex raw, latex booktabs, and textile table generation are also supported.
.
.
.
Written in Shanghai at 13:11:00 on March 25, 2019