Texttable
Texttable is a Python module to create simple ASCII tables. It provides an easy-to-use API for formatting data into human-readable text tables, supporting various alignments, data types, and decorative options. The current version is 1.7.0, and it is actively maintained with regular updates.
Warnings
- gotcha Automatic column data type detection can lead to unexpected formatting, especially for numbers like `1.0` or `0.0` which might be converted to integers instead of floats. Always explicitly set column data types using `set_cols_dtype()` if precise formatting is needed.
- gotcha The `max_width` parameter in the `Texttable` constructor can cause cell content to wrap if the content exceeds the calculated column width. Setting `max_width=0` allows the table to self-adjust and prevents wrapping, ensuring all content appears on a single line where possible.
- breaking Version 1.6.5 introduced a regression where stub files were missing in the wheel package, potentially causing import or packaging issues for some users.
- deprecated The behavior of boolean values in table cells was improved with a dedicated formatting option in version 1.7.0. Older versions might not format booleans as intuitively or consistently.
Install
-
pip install texttable
Imports
- Texttable
from texttable import Texttable
Quickstart
from texttable import Texttable table = Texttable() # Set column alignments (left, right, center) table.set_cols_align(["l", "r", "c"]) # Set column data types (text, float, integer) table.set_cols_dtype(["t", "f", "i"]) # Add a header row table.header(["Name", "Score", "Rank"]) # Add rows of data table.add_row(["Alice", 95.5, 1]) table.add_row(["Bob", 88.0, 2]) table.add_row(["Charlie", 72.3, 3]) # Draw and print the table print(table.draw())