📜  使用 PrettyTable 库创建表 - Python(1)

📅  最后修改于: 2023-12-03 15:06:49.821000             🧑  作者: Mango

使用 PrettyTable 库创建表 - Python

PrettyTable 是一款功能强大的 Python 库,它可以让你快速创建漂亮的表格,并支持多种格式输出。

安装 PrettyTable

可以使用 pip 命令进行安装:

pip install prettytable
创建表格

以下是一个简单的创建表格的示例:

from prettytable import PrettyTable

x = PrettyTable()

x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
x.add_row(["Adelaide",1295, 1158259, 600.5])
x.add_row(["Brisbane",5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])

print(x)

输出结果如下:

+------------+------+------------+-----------------+
| City name  | Area | Population | Annual Rainfall |
+------------+------+------------+-----------------+
|  Adelaide | 1295 |  1158259   |      600.5      |
|  Brisbane | 5905 |  1857594   |      1146.4     |
|   Darwin   | 112  |   120900   |      1714.7     |
|   Hobart   | 1357 |   205556   |      619.5      |
|   Sydney   | 2058 |  4336374   |      1214.8     |
| Melbourne | 1566 |  3806092   |      646.9      |
+------------+------+------------+-----------------+
自定义表格样式

可以使用 set_style() 方法自定义表格样式,以下是一个示例:

from prettytable import PrettyTable

x = PrettyTable()

x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
x.add_row(["Adelaide",1295, 1158259, 600.5])
x.add_row(["Brisbane",5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])

x.set_style("grid")

print(x)

输出结果如下:

+------------+------+------------+-----------------+
| City name  | Area | Population | Annual Rainfall |
+------------+------+------------+-----------------+
|  Adelaide | 1295 |  1158259   |      600.5      |
+------------+------+------------+-----------------+
|  Brisbane | 5905 |  1857594   |      1146.4     |
+------------+------+------------+-----------------+
|   Darwin   | 112  |   120900   |      1714.7     |
+------------+------+------------+-----------------+
|   Hobart   | 1357 |   205556   |      619.5      |
+------------+------+------------+-----------------+
|   Sydney   | 2058 |  4336374   |      1214.8     |
+------------+------+------------+-----------------+
| Melbourne | 1566 |  3806092   |      646.9      |
+------------+------+------------+-----------------+
输出为文件

可以使用 write() 方法将表格输出到文件中,以下是一个示例:

from prettytable import PrettyTable

x = PrettyTable()

x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
x.add_row(["Adelaide",1295, 1158259, 600.5])
x.add_row(["Brisbane",5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])

x.set_style("grid")

with open('output.txt', 'w') as f:
    f.write(str(x))

这将把表格输出到名为 output.txt 的文件中。

结论

PrettyTable 是一款强大的 Python 库,它可以让你快速创建漂亮的表格,并支持多种格式输出。它非常易于使用,并且具有丰富的自定义功能,可以满足各种表格输出需求。