PrettyTable 0.4
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.4 (released May 13, 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.set_padding_width(1) # One space between column edges and contents (default)
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 |
+-----------+------+------------+-----------------+
You can use x.add_column to build the table up column-by-column instead of row-by-row, if you're so inclined.
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 |
+-----------+------+------------+-----------------+
You can sort the rows of your table by a particular column like this:
x.printt(sortby="Annual Rainfall")
which gives you this:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Hobart | 1357 | 205556 | 619.5 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Sydney | 2058 | 4336374 | 1214.8 |
| 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).
Instead of an ASCII table, you can call print_HTML to print out a HTML <table> structure or get_HTML_string to get a string copy of what would be printed.
There are some other methods for controlling fine details of the printing process, that you can read about in the complete API (see below).
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.4 - May 13, 2009 (Download)
- Added add_column method to enable building tables up column-by-column.
- Added print_HTML and get_HTML_string methods to enable HTML table production.
- Added set_border_chars method to enable control over characters used to draw the table border.
- Added set_left_padding and set_right_padding methods to allow independent padding control for both sides of a column.
- Added sortby option to enable column sorting.
- Added header option to enable switching off field name printing at top of table.
- Modified hrules option to enable greater control over presence of horizontal lines.
- Added border option to enable switching off all line printing.
Thanks to Tim Cera, Chris Clark, Alexander Lamaison for suggesting and helping to test many of the new features in this release.
PrettyTable 0.3 - May 01, 2009 (Download) (Webpage)
- Added padding_width option to control the number of spaces between the vertical line rules at the edges of a column and its content. This can be set as a keyword argument to the constructor or after instantiation using the set_padding_width method. The value is set to 1 by defaut. If your table is too wide for a small screen with this value, setting it to 0 might help you squeeze it in.
Thanks to Chris Clark for contributing a patch against 0.2.1 to add this feature!
PrettyTable 0.2.1 - April 29, 2009 (Download) (Webpage)
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) (Webpage)
- 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).
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.