Python library and command line utility to pretty-print tabular data
Project description
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.
Installation
tabletext is available on Pypi and can be installed with:
$ pip install tabletext
Overview
Library
tabletext exposes a single function, to_text which in its simplest form takes a list of list (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.
Command line utility
The command line utility reads from its input the table, each line representing a row, its 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.
Documentation
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 and header_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.
Release history
0.1 (2014-08-14)
Initial release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file tabletext-0.1.tar.gz
.
File metadata
- Download URL: tabletext-0.1.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fd53c6acb9641cf0b6641ff9675ce3818d4b0541b9e05e8219c90ffc8512337 |
|
MD5 | dd948785e04a25772af103ad6eba1795 |
|
BLAKE2b-256 | ed534152a8c71531d4b09eb34cfd7cd18f3764a988de4dc9039405f8182dddb7 |