[ Python ] 常用类库学习之 tabulate

tabulate

作用:打印漂亮的表格数据

安装:pip install tabulate

文档:https://pypi.org/project/tabulate/


一个简单地示例

  • 我觉得其中最好看的一个表格(哈哈哈)
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 |
+--------+-----------+--------+-----------+

Table format 的可选参数

  • 这个参数用来设置表格形状的
"plain"
"simple"
"github"
"grid"
"fancy_grid"
"pipe"
"orgtbl"
"jira"
"presto"
"psql"
"rst"
"mediawiki"
"moinmoin"
"youtrack"
"html"
"latex"
"latex_raw"
"latex_booktabs"
"textile"

表格形状

  • plain
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(">>> plain")
print(tabulate(table_data, headers=table_header, tablefmt='plain'))

>>> plain
Name      Chinese    Math    English
Tom            90      80         85
Jim            70      90         80
Lucy           90      70         90
  • simple
print(">>> simple")
print(tabulate(table_data, headers=table_header, tablefmt='simple'))

>>> simple
Name      Chinese    Math    English
------  ---------  ------  ---------
Tom            90      80         85
Jim            70      90         80
Lucy           90      70         90
  • fancy_grid
print(">>> fancy_grid")
print(tabulate(table_data, headers=table_header, tablefmt='fancy_grid'))

>>> fancy_grid
╒════════╤═══════════╤════════╤═══════════╕
│ Name   │   Chinese │   Math │   English │
╞════════╪═══════════╪════════╪═══════════╡
│ Tom    │        908085 │
├────────┼───────────┼────────┼───────────┤
│ Jim    │        709080 │
├────────┼───────────┼────────┼───────────┤
│ Lucy   │        907090 │
╘════════╧═══════════╧════════╧═══════════╛
  • pipe
print(">>> pipe")
print(tabulate(table_data, headers=table_header, tablefmt='pipe'))

>>> pipe
| Name   |   Chinese |   Math |   English |
|:-------|----------:|-------:|----------:|
| Tom    |        90 |     80 |        85 |
| Jim    |        70 |     90 |        80 |
| Lucy   |        90 |     70 |        90 |
  • orgtbl
print(">>> orgtbl")
print(tabulate(table_data, headers=table_header, tablefmt='orgtbl'))

>>> orgtbl
| Name   |   Chinese |   Math |   English |
|--------+-----------+--------+-----------|
| Tom    |        90 |     80 |        85 |
| Jim    |        70 |     90 |        80 |
| Lucy   |        90 |     70 |        90 |
  • jira
print(">>> jira")
print(tabulate(table_data, headers=table_header, tablefmt='jira'))

>>> jira
|| Name   ||   Chinese ||   Math ||   English ||
| Tom    |        90 |     80 |        85 |
| Jim    |        70 |     90 |        80 |
| Lucy   |        90 |     70 |        90 |
  • presto
print(">>> presto")
print(tabulate(table_data, headers=table_header, tablefmt='presto'))

>>> presto
 Name   |   Chinese |   Math |   English
--------+-----------+--------+-----------
 Tom    |        90 |     80 |        85
 Jim    |        70 |     90 |        80
 Lucy   |        90 |     70 |        90
  • psql
print(">>> psql")
print(tabulate(table_data, headers=table_header, tablefmt='psql'))

>>> psql
+--------+-----------+--------+-----------+
| Name   |   Chinese |   Math |   English |
|--------+-----------+--------+-----------|
| Tom    |        90 |     80 |        85 |
| Jim    |        70 |     90 |        80 |
| Lucy   |        90 |     70 |        90 |
+--------+-----------+--------+-----------+
  • rst
print(">>> rst")
print(tabulate(table_data, headers=table_header, tablefmt='rst'))

>>> rst
======  =========  ======  =========
Name      Chinese    Math    English
======  =========  ======  =========
Tom            90      80         85
Jim            70      90         80
Lucy           90      70         90
======  =========  ======  =========
  • html
print(">>> html")
print(tabulate(table_data, headers=table_header, tablefmt='html'))

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

在这里插入图片描述

发布了144 篇原创文章 · 获赞 2297 · 访问量 50万+

猜你喜欢

转载自blog.csdn.net/qq_43901693/article/details/104920856