tabletext
is a Python library to format (pretty-print) tabular data as text
tables. Its goal is to be as simple as possible, while allowing optional
customization of the output.
tabletext
also comes with a command line utility, table
which formats
its input into a table and prints it on the standard output.
tabletext
is available on Pypi and can be installed with:
$ pip install tabletext
tabletext
exposes a single function, to_text
which in its simplest form
takes a list of lists (or any sequence of sequences) and format it as
a table. The data is assumed to be in row-major order, meaning that the
outer sequence's elements are the rows of the table.
>>> from tabletext import to_text
>>> a = [["Code", "Name"],
["AD", "ANDORRA"],
["AE", "UNITED ARAB EMIRATES"],
["AF", "AFGHANISTAN"],
["AG", "ANTIGUA AND BARBUDA"]]
>>> print to_text(a)
will output the following:
┌──────┬──────────────────────┐
│ Code │ Name │
├──────┼──────────────────────┤
│ AD │ ANDORRA │
├──────┼──────────────────────┤
│ AE │ UNITED ARAB EMIRATES │
├──────┼──────────────────────┤
│ AF │ AFGHANISTAN │
├──────┼──────────────────────┤
│ AG │ ANTIGUA AND BARBUDA │
└──────┴──────────────────────┘
You can customize the output with optional arguments:
>>> print to_text(a, header=True, corners="+", hor="-", ver="|",
header_corners="+", header_hor="=",
header_ver="!", formats=[">", "<"])
will output:
+======+======================+
! Code ! Name !
+======+======================+
| AD | ANDORRA |
+------+----------------------+
| AE | UNITED ARAB EMIRATES |
+------+----------------------+
| AF | AFGHANISTAN |
+------+----------------------+
| AG | ANTIGUA AND BARBUDA |
+------+----------------------+
| AI | ANGUILLA |
+------+----------------------+
See the Documentation section for more details about the optional arguments of
the to_text
function.
The command line utility reads the table from its input, each line representing
a row, the row's entries being separated by \t
characters (configurable),
and outputs the formatted table to the standard output:
$ df -h | tr -s ' ' \\t | cut -f1-6 | table --header
╒════════════╤══════╤══════╤═══════╤══════╤════════════════╕
│ Filesystem │ Size │ Used │ Avail │ Use% │ Mounted │
╞════════════╪══════╪══════╪═══════╪══════╪════════════════╡
│ /dev/sda2 │ 25G │ 14G │ 9.5G │ 60% │ / │
├────────────┼──────┼──────┼───────┼──────┼────────────────┤
│ dev │ 3.8G │ 0 │ 3.8G │ 0% │ /dev │
├────────────┼──────┼──────┼───────┼──────┼────────────────┤
│ run │ 3.8G │ 756K │ 3.8G │ 1% │ /run │
├────────────┼──────┼──────┼───────┼──────┼────────────────┤
│ tmpfs │ 3.8G │ 1.3M │ 3.8G │ 1% │ /dev/shm │
├────────────┼──────┼──────┼───────┼──────┼────────────────┤
│ tmpfs │ 3.8G │ 0 │ 3.8G │ 0% │ /sys/fs/cgroup │
├────────────┼──────┼──────┼───────┼──────┼────────────────┤
│ /dev/sda1 │ 511M │ 24M │ 488M │ 5% │ /boot │
├────────────┼──────┼──────┼───────┼──────┼────────────────┤
│ tmpfs │ 3.8G │ 372M │ 3.5G │ 10% │ /tmp │
├────────────┼──────┼──────┼───────┼──────┼────────────────┤
│ /dev/sda3 │ 15G │ 9.8G │ 4.2G │ 71% │ /home │
├────────────┼──────┼──────┼───────┼──────┼────────────────┤
│ /dev/sda5 │ 77G │ 46G │ 27G │ 64% │ /media/data │
├────────────┼──────┼──────┼───────┼──────┼────────────────┤
│ tmpfs │ 774M │ 16K │ 774M │ 1% │ /run/user/1000 │
└────────────┴──────┴──────┴───────┴──────┴────────────────┘
The available options can be printed with table --help
and closely follow
the optional arguments of the library's to_text
function as documented
here.
The optional arguments of the to_text
function are as follows:
Argument | Default | Description |
---|---|---|
formats |
None |
Format strings for the entries (see below) |
padding |
(1, 1) |
Left and right padding lengths |
corners |
"┌┬┐├┼┤└┴┘" |
Characters to use for the corners |
hor |
"─" |
Horizontal separation character |
ver |
"│" |
Vertical separation character |
header |
False |
Wether or not to display the first row as a header row |
header_corners |
"╒╤╕╞╪╡" |
Characters to use for the header row corners |
header_hor |
"═" |
Horizontal separation character for the header row |
header_ver |
"│" |
Vertical separation character for the header row |
More about some options:
formats
can be either a single format string, in which case it will be used for all entries, or a list of format strings, one per column of the table. The format strings must follow Python's format specification. Note however that you don't have to specify the width since it is automatically computed. Useful format strings are"<"
,">"
and"^"
for left-aligned, right-aligned and centered columns respectively.corners
andheader_corners
are strings containing the corner characters to be used for rows and the header row respectively. Follow the same order as in the default values. Alternatively, you can specify only one character (that is, a string of length 1) which will be used for all corners.