Python Library: tabulate

I prefer to use Restructured Text for my posts, but drawing tables in Restructured text has always been tricky for me. Getting the columns right, drawing and aligning the bar characters is a frustrating exercise for me.

The best to draw tables is to then generate it. I've tried ascii art table maker and I have not been very impressed or successful with it.

I tried a python library called tabulate [1] which can be used to print the table in restructed text format. The table could easily be constructed using python data structures like list.

Here is an example table of Graph applications, which shows the concept graphs, their vertices and edges.

graph vertex edge
communication telephone, computer fiber optic cable
circuit gate, register, processor wire
mechanical joint rod, beam, spring
financial stock, currency transactions
transportation street intersection, airport highway, airway route
internet class C network connection
game board position legal move
social relationship person, actor friendship, movie cast
neural network neuron synapse
protein network protein protein-protein interaction
molecule atom bond

This table was generated using this python program.

python/ (Source)

from tabulate import tabulate

table_header = ["graph", "vertex", "edge"]

items = [
    ["communication", "telephone, computer", "fiber optic cable"],
    ["circuit", "gate, register, processor", "wire"],
    ["mechanical", "joint", "rod, beam, spring"],
    ["financial", "stock, currency", "transactions"],
    ["transportation", "street intersection, airport", "highway, airway route"],
    ["internet", "class C network", "connection"],
    ["game", "board position", "legal move"],
    ["social relationship", "person, actor", "friendship, movie cast"],
    ["neural network", "neuron", "synapse"],
    ["protein network", "protein", "protein-protein interaction"],
    ["molecule", "atom", "bond"]]

print(tabulate(items, headers=table_header, tablefmt="rst"))

I landed upon tabulate when another python library by name PTable [2] did not provide a restructured text specific formatting. If you just prefer ASCII tables, then that's a convenient library as well.



Comments powered by Disqus