PrettyTable 0.2.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.2.1 (released April 29, 2009).
If you have easy_install, you can also install the latest version of PrettyTable just by running easy_install prettytable (or sudo easy_install prettytable as appropriate).
Detailed description
PrettyTable is best explained by example. It is designed to let you write something like this:
x = PrettyTable(["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])
print x
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 (note the double 't' in "printt", which is necessary because "print" is a reserved word in Python):
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 |
+-----------+------+------------+-----------------+
If you don't want to actually print your table but instead just get a string of exactly what would be printed if you called printt (e.g. so you can save it to a file), then you can call the get_string method, which takes all the arguments of printt shown above (fields, start and end).
That's about it for now!
Complete API
You can examine the complete PrettyTable API here.
Old versions / History
The list below details all versions of PrettyTable which have ever been released, as well as summarising the changes between versions. You can also see how this webpage looked at the time of each release, which is handy for finding documentation for old releases.
PrettyTable 0.2.1 - April 29, 2009 (Download)
This is a bug fix release to take care of some serious problems in 0.2, which was obviously released with insufficient testing!
- Caching no longer breaks when using the printt(fields=[...]) syntax. The list of fields was not hashable and hence could not be used as a dictionary key. I fixed this using the output of the cPickle module's dumps function as the dictionary key instead.
- Horizontal lines are now the appropriate length when the above syntax is used.
Thanks to Julien Koesten for reporting these bugs and testing the fixes almost immediately after the release of 0.2!
PrettyTable 0.2 - April 29, 2009 (Download)
- Added get_string method.
- Added str method (which just calls get_string) to enable nice print x syntax.
- Can now pass field names as a constructor argument.
- Return values of get_string are cached in a dictionary that is only cleared after a call to add_row or something else which invalidates the cache.
PrettyTable 0.1 - February 26, 2009 (Download) (Webpage)
- The original release
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.