PrettyTable 0.1
PrettyTable is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables. It was inspired by the ASCII tables used in the PostgreSQL shell psql. PrettyTable allows for selection of which columns are to be printed, independent alignment of columns (left or right justified or centred) and printing of "sub-tables" by specifying a row range.
Requirements
PrettyTable has no requirements other than Python.
License
PrettyTable is distributed under a standard 3-clause BSD license. It's as free as software gets.
Download
Download the latest version of PrettyTable, PrettyTable 0.1 (released February 26, 2009).
Detailed description
PrettyTable is best explained by example. It is designed to let you write something like this:
x = PrettyTable()
x.set_field_names(["City name", "Area", "Population", "Annual Rainfall"])
x.set_field_align("City name", "l") # Left align city names
x.add_row(["Adelaide",1295, 1158259, 600.5])
x.add_row(["Brisbane",5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])
x.printt()
in order to get something 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 |
| Sydney | 2058 | 4336374 | 1214.8 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
+-----------+------+------------+-----------------+
If you're only interested in showing the city populations, you can do this:
x.printt(fields=["City name, "Population"])
and get this:
+-----------+------------+
| City name | Population |
+-----------+------------+
| Adelaide | 1158259 |
| Brisbane | 1857594 |
| Darwin | 120900 |
| Hobart | 205556 |
| Sydney | 4336374 |
| Melbourne | 3806092 |
| Perth | 1554769 |
+-----------+------------+
You can print only the first 3 rows of the table by doing this:
x.printt(start=0,end=3)
which gives you this:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
+-----------+------+------------+-----------------+
That's about it for now!
Complete API
You can examine the complete PrettyTable API here.
Possible future features
Maybe in time I'll expand PrettyTable to have to following features:
- Automatically break multi-word field names (like "Annual Rainfall") over two lines in the header if it will keep the overall table width less than 80 characters.
- Put commas in the appropriate places when printing large numbers (e.g. 1158259 printed as 1,158,259).
- Sort the rows by some column (perhaps by tying the table to an in-memory SQLite database?)
Feel free to beat me to implementing any of these features! Send me mail if you do.
Contribute
Bug reports and suggestion for improvement of PrettyTable are very welcome: just email them to me. Full credit will be given on this page for bug reports, fixes, etc. Feel free to email me even just to let me know you think PrettyTable is neat, too.