PrettyTable lets you print tables in an attractive ASCII form:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
| Sydney | 2058 | 4336374 | 1214.8 |
+-----------+------+------------+-----------------+
Install via pip:
python -m pip install -U prettytable
Install latest development version:
python -m pip install -U git+https://github.com/prettytable/prettytable
Or from requirements.txt
:
-e git://github.com/prettytable/prettytable.git#egg=prettytable
To see demo output, run:
python3 -m prettytable
Let's suppose you have a shiny new PrettyTable:
from prettytable import PrettyTable
table = PrettyTable()
and you want to put some data into it. You have a few options.
You can add data one row at a time. To do this you can set the field names first using
the field_names
attribute, and then add the rows one at a time using the add_row
method:
table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
table.add_row(["Adelaide", 1295, 1158259, 600.5])
table.add_row(["Brisbane", 5905, 1857594, 1146.4])
table.add_row(["Darwin", 112, 120900, 1714.7])
table.add_row(["Hobart", 1357, 205556, 619.5])
table.add_row(["Sydney", 2058, 4336374, 1214.8])
table.add_row(["Melbourne", 1566, 3806092, 646.9])
table.add_row(["Perth", 5386, 1554769, 869.4])
When you have a list of rows, you can add them in one go with add_rows
:
table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
table.add_rows(
[
["Adelaide", 1295, 1158259, 600.5],
["Brisbane", 5905, 1857594, 1146.4],
["Darwin", 112, 120900, 1714.7],
["Hobart", 1357, 205556, 619.5],
["Sydney", 2058, 4336374, 1214.8],
["Melbourne", 1566, 3806092, 646.9],
["Perth", 5386, 1554769, 869.4],
]
)
You can add data one column at a time as well. To do this you use the add_column
method, which takes two arguments - a string which is the name for the field the column
you are adding corresponds to, and a list or tuple which contains the column data:
table.add_column("City name",
["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"])
table.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386])
table.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092,
1554769])
table.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9,
869.4])
If you really want to, you can even mix and match add_row
and add_column
and build
some of your table in one way and some of it in the other. Tables built this way are
kind of confusing for other people to read, though, so don't do this unless you have a
good reason.
If you have your table data in a comma-separated values file (.csv), you can read this data into a PrettyTable like this:
from prettytable import from_csv
with open("myfile.csv") as fp:
mytable = from_csv(fp)
If you have your table data in a database which you can access using a library which
confirms to the Python DB-API (e.g. an SQLite database accessible using the sqlite
module), then you can build a PrettyTable using a cursor object, like this:
import sqlite3
from prettytable import from_db_cursor
connection = sqlite3.connect("mydb.db")
cursor = connection.cursor()
cursor.execute("SELECT field1, field2, field3 FROM my_table")
mytable = from_db_cursor(cursor)
There are three ways to get data out of a PrettyTable, in increasing order of completeness:
- The
del_row
method takes an integer index of a single row to delete. - The
del_column
method takes a field name of a single column to delete. - The
clear_rows
method takes no arguments and deletes all the rows in the table - but keeps the field names as they were so you that you can repopulate it with the same kind of data. - The
clear
method takes no arguments and deletes all rows and all field names. It's not quite the same as creating a fresh table instance, though - style related settings, discussed later, are maintained.
PrettyTable's main goal is to let you print tables in an attractive ASCII form, like this:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
| Sydney | 2058 | 4336374 | 1214.8 |
+-----------+------+------------+-----------------+
You can print tables like this to stdout
or get string representations of them.
To print a table in ASCII form, you can just do this:
print(table)
The old table.printt()
method from versions 0.5 and earlier has been removed.
To pass options changing the look of the table, use the get_string()
method documented
below:
print(table.get_string())
If you don't want to actually print your table in ASCII form but just get a string
containing what would be printed if you use print(table)
, you can use the
get_string
method:
mystring = table.get_string()
This string is guaranteed to look exactly the same as what would be printed by doing
print(table)
. You can now do all the usual things you can do with a string, like write
your table to a file or insert it into a GUI.
The table can be displayed in several different formats using get_formatted_string
by
changing the out_format=<text|html|json|csv|latex|mediawiki>
. This function passes
through arguments to the functions that render the table, so additional arguments can be
given. This provides a way to let a user choose the output formatting.
def my_cli_function(table_format: str = 'text'):
...
print(table.get_formatted_string(table_format))
If you like, you can restrict the output of print(table)
or table.get_string
to only
the fields or rows you like.
The fields
argument to these methods takes a list of field names to be printed:
print(table.get_string(fields=["City name", "Population"]))
gives:
+-----------+------------+
| City name | Population |
+-----------+------------+
| Adelaide | 1158259 |
| Brisbane | 1857594 |
| Darwin | 120900 |
| Hobart | 205556 |
| Melbourne | 3806092 |
| Perth | 1554769 |
| Sydney | 4336374 |
+-----------+------------+
The start
and end
arguments take the index of the first and last row to print
respectively. Note that the indexing works like Python list slicing - to print the 2nd,
3rd and 4th rows of the table, set start
to 1 (the first row is row 0, so the second
is row 1) and set end
to 4 (the index of the 4th row, plus 1):
print(table.get_string(start=1, end=4))
prints:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
+-----------+------+------------+-----------------+
You can make sure that your tables are filtered by giving get_string
a row_filter
keyword argument, which must be a function with one argument row
returning a Boolean
value. The row
is the list of fields in a row.
For example, to print the example table we built earlier of Australian capital city data, so that cities with a population of at least 1,000,000, we can do this:
def filter_function(row: list[str]) -> bool:
return row[2] > 999999
print(table.get_string(row_filter=filter_function))
to get:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Sydney | 2058 | 4336374 | 1214.8 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
+-----------+------+------------+-----------------+
By default, all columns in a table are centre aligned.
You can change the alignment of all the columns in a table at once by assigning a one
character string to the align
attribute. The allowed strings are "l"
, "r"
and
"c"
for left, right and centre alignment, respectively:
table.align = "r"
print(table)
gives:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
| Sydney | 2058 | 4336374 | 1214.8 |
+-----------+------+------------+-----------------+
You can also change the alignment of individual columns based on the corresponding field
name by treating the align
attribute as if it were a dictionary.
table.align["City name"] = "l"
table.align["Area"] = "c"
table.align["Population"] = "r"
table.align["Annual Rainfall"] = "c"
print(table)
gives:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
| Sydney | 2058 | 4336374 | 1214.8 |
+-----------+------+------------+-----------------+
You can make sure that your ASCII tables are produced with the data sorted by one
particular field by giving get_string
a sortby
keyword argument, which must be a
string containing the name of one field.
For example, to print the example table we built earlier of Australian capital city data, so that the most populated city comes last, we can do this:
print(table.get_string(sortby="Population"))
to get:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Adelaide | 1295 | 1158259 | 600.5 |
| Perth | 5386 | 1554769 | 869.4 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Sydney | 2058 | 4336374 | 1214.8 |
+-----------+------+------------+-----------------+
If we want the most populated city to come first, we can also give a
reversesort=True
argument.
If you always want your tables to be sorted in a certain way, you can make the setting long-term like this:
table.sortby = "Population"
print(table)
print(table)
print(table)
All three tables printed by this code will be sorted by population (you could do
table.reversesort = True
as well, if you wanted). The behaviour will persist until you
turn it off:
table.sortby = None