Python alignment display | | | |, tabuilate function |, wcwidth module |, that's enough to know

Keywords: Python github

tabulate literal translation is to make tables and let python realize tabular display.

Detailed documentation

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"
  • Common functions

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.

  • Export table styles

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.

  • Reference resources

  1. Python language Brochure

.
.
.
Written in Shanghai at 13:11:00 on March 25, 2019

Posted by Eskimo887 on Thu, 05 Dec 2019 02:12:10 -0800