tabulate

Utilities

漂亮的印刷表格

概述 tabulate

Tabulate 是一个 Python 库,用于将表格数据漂亮地打印到终端。它支持多种输出格式,包括纯文本、网格、管道 (Markdown)、HTML、LaTeX 等。它适用于列表、字典、PandasDataFrame 和NumPy数组的列表。

在PyRun中,制表可通过 micropip 使用,是在终端面板中清晰呈现数据输出的绝佳方式。它通常与Pandas一起使用,以人类可读的格式显示 DataFrame,或从 Python 数据生成 Markdown 表。

代码和执行输出

以网格、大纲和 Markdown 格式漂亮地打印数据。

Tabulate Tables在编辑器中运行
from tabulate import tabulate

data = [
    ["Alice",   28, "Engineer",  "New York"],
    ["Bob",     34, "Designer",  "London"],
    ["Charlie", 22, "Data Sci.", "Berlin"],
    ["Diana",   30, "Manager",   "Tokyo"],
]
headers = ["Name", "Age", "Role", "City"]

print("── grid ──")
print(tabulate(data, headers=headers, tablefmt="grid"))

print("\n── rounded_outline ──")
print(tabulate(data, headers=headers, tablefmt="rounded_outline"))

print("\n── pipe (Markdown) ──")
print(tabulate(data, headers=headers, tablefmt="pipe"))

相关套餐

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。