Tabulate 0.7.1 with LaTeX & MediaWiki tables

2013-12-12

Recently I've been pushing to Bitbucket and GitHub more often than updating this blog. This time I'd like announce that since my last post (Pretty-printing tables in Python), tabulate library went a long way forward. It has attracted quite a few of users, and even some contributors. I'm surprised myself.

Yesterday I've released tabulate 0.7.1. With respect to the version I blogged about nine months ago, it has got several new features, and many bug fixes. Internally it has changed a lot, the API exposed to the user stays the same: just one function, which magically works out the best way to print tabular data. It remains a one-file library.

It can print more types of tabular data now, not just lists of lists and NumPy arrays, but also

  • Dictionaries of lists of other iterables,
  • Lists (or other iterables) of named tuples,
  • NumPy record arrays,
  • Pandas data frames.

It can now derive table headers from the data itself (use headers="firstrow" or headers="keys" argument of the tabulate() function). This feature is very handy when working with new types of tabular data.

For instance, NumPy record arrays may define names for the elements of the records; tabulate() can re-use their names as headers:

>>> import numpy ; from tabulate import tabulate ;
>>> recarr = numpy.asarray([("Sun", 696000, 1.9891e9), ("Earth", 6371, 5973.6)],
...                        dtype={"names": ("body", "r [km]", "mass [x 1e29 kg]"),
...                               "formats": ("a18", "int32", "float32")})

>>> recarr
array([('Sun', 696000, 1989100032.0), ('Earth', 6371, 5973.60009765625)],
      dtype=[('body', 'S18'), ('r [km]', '<i4'), ('mass [x 1e29 kg]', '<f4')])

>>> print tabulate(recarr, headers="keys")
body      r [km]    mass [x 1e29 kg]
------  --------  ------------------
Sun       696000          1.9891e+09
Earth       6371       5973.6

It can print more data types too, in addition to strings and numbers, it now properly prints

  • time and date values,
  • missing (None) values,
  • values with ANSI color sequences.

It works better on more machines. The following language versions are supported:

  • Python 2.6 (even as old as before 2.6.5)
  • Python 2.7
  • Python 3.3

Python 3.0, 3.1, and 3.2 are not supported because these versions of the language lack unicode literals.

There are more output formats. In addition to plain, simple (Pandoc), grid (table.el), pipe (PHP Markdown Extra), orgtbl (Org-mode), and rst (ReStructuredText) formats, there are also

  • mediawiki: paste your tables to Wikipedia
  • latex: generate a tabular environment for LaTeX

The former may make the word a better place. The latter will definitely make your data more trustworthy. There is also a simple_separated_format() function to create custom formats which use an arbitrary delimiter.

Now there is also an option to disable column alignment with spaces: set stralign and numalign options to None. This might be useful to generate output for automatic processing, but keep in mind that standard Python module csv is better and faster at generating CSV-like output.

I'd like to thank Erwin Marsi, Bill Ryder, Pau Tallada CrespĂ­, Mik Kocikowski, Boris Kuchin, and several anonymous users for their feedback, bug reports, and patches.

Tabulate is pip-installable (pip install tabulate), and available from PyPI and on Bitbucket:

For future updates about tabulate see #tabulate tag or subscribe to tabulate Atom feed.