Python tableprint 使用教程
1. 什么是 tableprint?
tableprint
是一个 Python 库,用于在终端或控制台中以美观的 ASCII 表格格式打印数据。它特别适合打印数值数据或结构化数据,支持动态添加行或一次性打印整个表格。常见的应用场景包括数据分析、计算结果展示等。
2. 安装 tableprint
要使用 tableprint
,首先需要安装它。可以通过 pip
安装:
pip install tableprint
安装完成后,您可以在 Python 脚本中导入它。
3. 基本用法
tableprint
提供了几种主要函数,用于生成和打印表格:
tableprint.table()
:一次性打印整个表格。tableprint.header()
、tableprint.row()
、tableprint.bottom()
:逐步构建表格。tableprint.banner()
:打印装饰性标题。
以下是具体用法和示例:
3.1 使用 table()
一次性打印表格
tableprint.table()
接受一个数据矩阵(通常是列表或 NumPy 数组)和标题列表,直接打印整个表格。
import tableprint as tp
import numpy as np
# 准备数据:10行3列的随机数
data = np.random.randn(10, 3)
headers = ['Column A', 'Column B', 'Column C']
# 打印表格
tp.table(data, headers)
输出示例(实际数字会因随机生成而不同):
╭────────────┬────────────┬────────────╮
│ Column A │ Column B │ Column C │
├────────────┼────────────┼────────────┤
│ 0.123 │ -0.456 │ 1.789 │
│ -0.987 │ 0.654 │ -0.321 │
│ ... │ ... │ ... │
╰────────────┴────────────┴────────────╯
data
:二维数据,可以是列表或 NumPy 数组。headers
:列标题列表,长度需与数据列数匹配。- 可选参数:
width
:每列宽度(默认 11)。style
:表格样式(默认 ‘round’,可选 ‘fancy_grid’, ‘grid’, ‘clean’, ‘block’)。
3.2 动态构建表格
如果您需要在循环中逐步打印表格(例如实时计算结果),可以使用 header()
、row()
和 bottom()
。
import tableprint as tp
import numpy as np
headers = ['A', 'B', 'C']
# 打印表头
print(tp.header(headers))
# 逐行打印数据
for i in range(5):
data = np.random.randn(3) # 模拟实时生成的数据
print(tp.row(data), flush=True) # flush=True 确保立即输出
# 打印表格底部
print(tp.bottom(3)) # 参数为列数
输出示例:
╭────────────┬────────────┬────────────╮
│ A │ B │ C │
├────────────┼────────────┼────────────┤
│ 0.234 │ -1.567 │ 0.890 │
│ -0.345 │ 0.123 │ -2.678 │
│ ... │ ... │ ... │
╰────────────┴────────────┴────────────╯
这种方法适合动态数据场景,例如实时监控或逐步计算。
3.3 使用 banner()
打印标题
如果只需要打印一个装饰性标题,可以使用 banner()
:
import tableprint as tp
tp.banner("Hello, Tableprint!")
输出示例:
******************************
* Hello, Tableprint! *
******************************
width
:控制宽度(默认 30)。style
:样式(默认 ‘banner’)。
4. 自定义选项
tableprint
支持一些自定义参数:
width
:设置每列宽度。例如:tp.table(data, headers, width=15)
style
:改变表格外观。支持的样式包括:'round'
(默认):圆角边框。'fancy_grid'
:带网格线的复杂样式。'grid'
:简单网格。'clean'
:无边框。'block'
:块状边框。
示例:
tp.table(data, headers, style='fancy_grid')
format_spec
:控制数字格式(默认 ‘5g’,表示 5 位有效数字)。例如:tp.table(data, headers, format_spec='.2f') # 保留两位小数
5. 注意事项
- 数据必须是二维结构,且每行长度需与标题数量一致。
- 如果使用 NumPy,确保导入
numpy
模块。 - 输出默认在标准输出(stdout),可以通过
out
参数重定向到文件(高级用法)。
6. 完整示例
以下是一个综合示例,展示多种功能:
import tableprint as tp
import numpy as np
# 数据
data = np.array([[1.2345, 2.3456, 3.4567],
[4.5678, 5.6789, 6.7890]])
headers = ['X', 'Y', 'Z']
# 打印标题
tp.banner("My Data Table", width=20)
# 打印表格(自定义宽度和样式)
tp.table(data, headers, width=10, style='grid', format_spec='.2f')
输出示例:
********************
* My Data Table *
********************
+----------+----------+----------+
| X | Y | Z |
+----------+----------+----------+
| 1.23 | 2.35 | 3.46 |
+----------+----------+----------+
| 4.57 | 5.68 | 6.79 |
+----------+----------+----------+
总结
tableprint
是一个简单而强大的工具,适合在终端中美观地展示表格数据。通过 table()
可以快速打印完整表格,通过 header()
、row()
、bottom()
可以动态构建,而 banner()
则提供装饰性标题。