Skip to main content

philiprehberger-text-table

Tests PyPI version Last updated

philiprehberger-text-table

Render data as clean ASCII/Unicode tables in the terminal with zero configuration.

Installation

pip install philiprehberger-text-table

Usage

from philiprehberger_text_table import table

headers = ["Name", "Age", "City"]
rows = [
    ["Alice", 30, "New York"],
    ["Bob", 25, "London"],
    ["Charlie", 35, "Tokyo"],
]

print(table(headers, rows))

Output:

┌─────────┬─────┬──────────┐
│ Name    │ Age │ City     │
├─────────┼─────┼──────────┤
│ Alice   │  30 │ New York │
│ Bob     │  25 │ London   │
│ Charlie │  35 │ Tokyo    │
└─────────┴─────┴──────────┘

From dictionaries

from philiprehberger_text_table import from_dicts

data = [
    {"name": "Alice", "score": 95},
    {"name": "Bob", "score": 87},
]

print(from_dicts(data))

From CSV

from philiprehberger_text_table import from_csv

print(from_csv("data.csv"))

Styles

# ASCII style
print(table(headers, rows, style="ascii"))

# Markdown style
print(table(headers, rows, style="markdown"))

# Minimal style (no borders)
print(table(headers, rows, style="minimal"))

# Compact style (no outer borders)
print(table(headers, rows, style="compact"))

# Rounded Unicode corners
print(table(headers, rows, style="rounded"))

# Double-line borders
print(table(headers, rows, style="double"))

Column alignment

# Override auto-detection for all columns
print(table(headers, rows, align="center"))

# Per-column alignment
print(table(headers, rows, align=["left", "center", "right"]))

From CSV string

from philiprehberger_text_table import from_csv_string

csv_data = "Name,Age\nAlice,30\nBob,25"
print(from_csv_string(csv_data))

From JSON

from philiprehberger_text_table import from_json, from_json_string

# List of dicts (keys become headers)
print(from_json_string('[{"name":"Alice","age":30},{"name":"Bob","age":25}]'))

# List of lists (first inner list is the header row)
print(from_json_string('[["Name","Age"],["Alice",30],["Bob",25]]'))

# Read from a file
print(from_json("data.json"))

Truncation

print(table(headers, rows, max_width=10))

Round-tripping CSV

from philiprehberger_text_table import to_csv, from_csv

csv_text = to_csv(
    [["Alice", 30], ["Bob", 25]],
    headers=["Name", "Age"],
    file="people.csv",  # optional — also writes to disk
)
print(csv_text)
# Name,Age
# Alice,30
# Bob,25

# Re-read and render
print(from_csv("people.csv"))

Round-tripping JSON

from philiprehberger_text_table import to_json, from_json_string

json_text = to_json(
    [["Alice", 30], ["Bob", 25]],
    headers=["name", "age"],  # omit for a plain array of arrays
)
print(json_text)
# [
#   {"name": "Alice", "age": 30},
#   {"name": "Bob", "age": 25}
# ]

# Re-render as a table
print(from_json_string(json_text))

Computing column widths

from philiprehberger_text_table import column_widths

widths = column_widths(["name", "count"], [["alice", 100], ["bob", 5]])
# [5, 5]  — max of header and cell str-lengths per column

API

Function / Class Description
table(headers, rows, *, style="unicode", max_width=None, align=None) Render a table from headers and row data
from_dicts(data, *, style="unicode", max_width=None, align=None) Render a table from a list of dictionaries
from_csv(path, *, style="unicode", max_width=None, align=None) Read a CSV file and render as a table
from_csv_string(text, *, style="unicode", max_width=None, align=None) Render a table from CSV string content
from_json(path, *, style="unicode", max_width=None, align=None) Read a JSON file and render as a table (list of dicts or list of lists)
from_json_string(text, *, style="unicode", max_width=None, align=None) Render a table from a JSON string
to_csv(rows, *, headers=None, file=None) Render rows back to a CSV string (round-trips with from_csv); optionally writes to a file
to_json(rows, *, headers=None, file=None, indent=2) Render rows to a JSON string (round-trips with from_json); optionally writes to a file
column_widths(headers, rows) Return the per-column widths the renderer would compute

Styles: "unicode", "rounded", "double", "ascii", "markdown", "minimal", "compact"

Alignments: "left", "right", "center" (default: auto-detect, numeric columns right-aligned)

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

⭐ Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Release files for philiprehberger-text-table 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for philiprehberger-text-table 0.5.0
File Size Uploaded
philiprehberger_text_table-0.5.0.tar.gz 196.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-text-table 0.5.0
File Interpreter ABI Platform
philiprehberger_text_table-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 204.4 kB

Release files / philiprehberger_text_table-0.5.0.tar.gz

Download URL philiprehberger_text_table-0.5.0.tar.gz
Size 196.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a4dc1d721ca1f37aced0faf5388eaa08dba0c0f7bae1bee02fbd07384d669c2f
BLAKE2b-256 checksum
How to use checksums
db96182c277f578856b04af8689f7f828e09db12007d4f75e7cb4bbbc8abb23e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_text_table-0.5.0-py3-none-any.whl

Download URL philiprehberger_text_table-0.5.0-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4b0af0d27e54e94348b603c9554cda32776cac411b1e4303106e091e66625aae
BLAKE2b-256 checksum
How to use checksums
de15cf68c1236f3b8a149c5134d338bdc59166eda24c48bac7d3943c3544b730
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page