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

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

详解文档_other

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模块

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'))
<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>

注意到, tabulate 函数也可以用来生成 html 表格定义代码。 此外,还支持 mediawikimoinmoinyoutracklatexlatex_rawlatex__booktabstextile 表格生成。

  • 参考

  1. Python语言小册子

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

猜你喜欢

转载自blog.csdn.net/The_Time_Runner/article/details/88791587