PTable
PTable is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables. It was originally forked from PrettyTable and maintains API compatibility. The current version is 0.9.2, with its last release in 2015, indicating a largely inactive or maintenance-only development cadence.
Common errors
-
ImportError: cannot import name 'PTable' from 'ptable'
cause The main class is named `PrettyTable` and is imported from the `prettytable` module, even when the package installed is `PTable`.fixUse `from prettytable import PrettyTable` instead of `from ptable import PTable`. -
AttributeError: 'PrettyTable' object has no attribute 'printt'
cause You are attempting to use the deprecated `printt()` method, which was removed in versions 0.6 and later.fixReplace `x.printt()` with `print(x)` or `print(x.get_string())`. -
Unexpected table formatting or missing features when both PTable and prettytable are installed.
cause Due to `PTable` being a fork of `PrettyTable` and using the same internal `prettytable` namespace, having both packages installed can cause Python to load an unpredictable version of the library, leading to inconsistent behavior.fixUninstall one of the packages. If you intend to use the `PTable` fork, ensure only `pip uninstall prettytable` is performed, then `pip install PTable`. Vice-versa if you prefer the original `prettytable`.
Warnings
- breaking The `x.printt()` method, available in versions 0.5 and earlier, has been removed.
- gotcha PTable uses the `prettytable` namespace for its imports (`from prettytable import PrettyTable`), which can lead to conflicts or unexpected behavior if the original `prettytable` library is also installed in the same environment.
- deprecated The library has not been updated since version 0.9.2 in May 2015. This indicates a lack of active maintenance and may lead to compatibility issues with newer Python versions or unaddressed bugs.
Install
-
pip install PTable
Imports
- PrettyTable
from ptable import PTable
from prettytable import PrettyTable
Quickstart
from prettytable import PrettyTable # Create a new table x = PrettyTable() # Set field names x.field_names = ["City name", "Area", "Population", "Annual Rainfall"] # Add rows x.add_row(["Adelaide", 1295, 1158259, 600.5]) x.add_row(["Brisbane", 5905, 1857594, 1146.4]) x.add_row(["Darwin", 112, 120900, 1714.7]) # Print the table (implicitly calls __str__ for ASCII representation) print(x) # Get HTML string representation html_table = x.get_html_string() # print(html_table) # Uncomment to see HTML output