用过 MySQL 的童鞋一定对那个非常好看的字符表格印象深刻吧!
在 Python 中,使用 tabulate 库,可以轻松实现一模一样的字符表格。
快速上手
废话不多说,先来看看效果:
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
 | >>> from tabulate import tabulate
>>> table_header = ['Name', 'Chinese', 'Math', 'English']
>>> table_data = [
...     ('Tom', '90', '80', '85'),
...     ('Jim', '70', '90', '80'),
...     ('Lucy', '90', '70', '90'),
... ]
>>> 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 |
+--------+-----------+--------+-----------+
 | 
 
上述代码,先从 tabulate 库导入同名工具函数;
接着,定义一个用于演示的数据表格,包括表头和表数据;
最后,用 tabulate 函数格式化表格,传参包括表数据,表头以及表格样式。
看到没有,引入 tabulate 函数后,只需要一行代码即可完成表格输出!
回过头来看看如何安装 tabulate 。你可能已经猜到了:
是的,就是这么简单!
中文对齐
默认没有考虑中文字符宽度,因此无法对齐:
| 1
2
3
 | +---------+--------+---------+--------+
| 工具      | 持仓     | 总成本     | 平均成本   |
+---------+--------+---------+--------+
 | 
 
解决这个问题只需安装 wcwidth 包:
并在代码中导入:
表格样式
tabulate 提供多种表格输出风格,列举如下:
plain
| 1
2
3
4
5
 | >>> 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
| 1
2
3
4
5
6
 | >>> 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
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
 | >>> 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
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
 | >>> 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
| 1
2
3
4
5
6
 | >>> 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
| 1
2
3
4
5
6
 | >>> 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
| 1
2
3
4
5
 | >>> 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
| 1
2
3
4
5
6
 | >>> 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
| 1
2
3
4
5
6
7
8
 | >>> 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
| 1
2
3
4
5
6
7
8
 | >>> 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
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
 | >>> 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 表格定义代码。
此外,还支持 mediawiki 、 moinmoin 、 youtrack 、 latex 、 latex_raw 、 latex__booktabs 、 textile 表格生成。
【小菜学Python】系列文章首发于公众号【小菜学编程】,敬请关注:
