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

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。