tabulate

用过 MySQL 的童鞋一定对那个非常好看的字符表格印象深刻吧!

Python 中,使用 tabulate 库,可轻松实现一模一样的字符表格。

快速上手

废话不多说,先来看看效果:

  1. >>> from tabulate import tabulate
  2.  
  3. >>> table_header = ['Name', 'Chinese', 'Math', 'English']
  4. >>> table_data = [
  5. ... ('Tom', '90', '80', '85'),
  6. ... ('Jim', '70', '90', '80'),
  7. ... ('Lucy', '90', '70', '90'),
  8. ... ]
  9.  
  10. >>> print(tabulate(table_data, headers=table_header, tablefmt='grid'))
  11. +--------+-----------+--------+-----------+
  12. | Name | Chinese | Math | English |
  13. +========+===========+========+===========+
  14. | Tom | 90 | 80 | 85 |
  15. +--------+-----------+--------+-----------+
  16. | Jim | 70 | 90 | 80 |
  17. +--------+-----------+--------+-----------+
  18. | Lucy | 90 | 70 | 90 |
  19. +--------+-----------+--------+-----------+

上述代码,先从 tabulate 库导入同名工具函数;接着,定义一个用于演示的数据表格,包括表头和表数据;最后,用 tabulate 函数格式化表格,传参包括表数据,表头以及表格样式。

看到没有,引入 tabulate 函数后,只需要一行代码即可完成表格输出!

回过头来看看如何安装 tabulate 。你可能已经猜到了:

  1. $ pip install tabulate

是的,就是这么简单!

中文对齐

默认没有考虑中文字符宽度,因此无法对齐:

  1. +---------+--------+---------+--------+
  2. | 工具 | 持仓 | 总成本 | 平均成本 |
  3. +---------+--------+---------+--------+

解决这个问题只需安装 wcwidth 包:

  1. $ pip install wcwidth

并在代码中导入:

  1. import wcwidth

表格样式

tabulate 提供多种表格输出风格,列举如下:

plain

  1. >>> print(tabulate(table_data, headers=table_header, tablefmt='plain'))
  2. Name Chinese Math English
  3. Tom 90 80 85
  4. Jim 70 90 80
  5. Lucy 90 70 90

simple

  1. >>> print(tabulate(table_data, headers=table_header, tablefmt='simple'))
  2. Name Chinese Math English
  3. ------ --------- ------ ---------
  4. Tom 90 80 85
  5. Jim 70 90 80
  6. Lucy 90 70 90

grid

  1. >>> print(tabulate(table_data, headers=table_header, tablefmt='grid'))
  2. +--------+-----------+--------+-----------+
  3. | Name | Chinese | Math | English |
  4. +========+===========+========+===========+
  5. | Tom | 90 | 80 | 85 |
  6. +--------+-----------+--------+-----------+
  7. | Jim | 70 | 90 | 80 |
  8. +--------+-----------+--------+-----------+
  9. | Lucy | 90 | 70 | 90 |
  10. +--------+-----------+--------+-----------+

fancy_grid

  1. >>> print(tabulate(table_data, headers=table_header, tablefmt='fancy_grid'))
  2. ╒════════╤═══════════╤════════╤═══════════╕
  3. Name Chinese Math English
  4. ╞════════╪═══════════╪════════╪═══════════╡
  5. Tom 90 80 85
  6. ├────────┼───────────┼────────┼───────────┤
  7. Jim 70 90 80
  8. ├────────┼───────────┼────────┼───────────┤
  9. Lucy 90 70 90
  10. ╘════════╧═══════════╧════════╧═══════════╛

pipe

  1. >>> print(tabulate(table_data, headers=table_header, tablefmt='pipe'))
  2. | Name | Chinese | Math | English |
  3. |:-------|----------:|-------:|----------:|
  4. | Tom | 90 | 80 | 85 |
  5. | Jim | 70 | 90 | 80 |
  6. | Lucy | 90 | 70 | 90 |

orgtlb

  1. >>> print(tabulate(table_data, headers=table_header, tablefmt='orgtbl'))
  2. | Name | Chinese | Math | English |
  3. |--------+-----------+--------+-----------|
  4. | Tom | 90 | 80 | 85 |
  5. | Jim | 70 | 90 | 80 |
  6. | Lucy | 90 | 70 | 90 |

jira

  1. >>> print(tabulate(table_data, headers=table_header, tablefmt='jira'))
  2. || Name || Chinese || Math || English ||
  3. | Tom | 90 | 80 | 85 |
  4. | Jim | 70 | 90 | 80 |
  5. | Lucy | 90 | 70 | 90 |

presto

  1. >>> print(tabulate(table_data, headers=table_header, tablefmt='presto'))
  2. Name | Chinese | Math | English
  3. --------+-----------+--------+-----------
  4. Tom | 90 | 80 | 85
  5. Jim | 70 | 90 | 80
  6. Lucy | 90 | 70 | 90

psql

  1. >>> print(tabulate(table_data, headers=table_header, tablefmt='psql'))
  2. +--------+-----------+--------+-----------+
  3. | Name | Chinese | Math | English |
  4. |--------+-----------+--------+-----------|
  5. | Tom | 90 | 80 | 85 |
  6. | Jim | 70 | 90 | 80 |
  7. | Lucy | 90 | 70 | 90 |
  8. +--------+-----------+--------+-----------+

rst

  1. >>> print(tabulate(table_data, headers=table_header, tablefmt='rst'))
  2. ====== ========= ====== =========
  3. Name Chinese Math English
  4. ====== ========= ====== =========
  5. Tom 90 80 85
  6. Jim 70 90 80
  7. Lucy 90 70 90
  8. ====== ========= ====== =========

html

  1. >>> print(tabulate(table_data, headers=table_header, tablefmt='html'))
  2. <table>
  3. <thead>
  4. <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>
  5. </thead>
  6. <tbody>
  7. <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>
  8. <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>
  9. <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>
  10. </tbody>
  11. </table>

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

下一步

订阅更新,获取更多学习资料,请关注我们的 微信公众号

../_images/wechat-mp-qrcode.png小菜学编程

微信打赏