Package: tabletext
tabletextis 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.tabletextalso comes with a command line utility,tablewhich formats
its input into a table and prints it on the standard output.Installationtabletextis available onPypiand can be installed with:$pipinstalltabletextOverviewLibrarytabletextexposes a single function,to_textwhich in its simplest form
takes a list of list (or anysequenceofsequences) and format it as a table.
The data is assumed to be inrow-major order, meaning that the outer
sequence’s elements are the rows of the table.>>>fromtabletextimportto_text>>>a=[["Code","Name"],["AD","ANDORRA"],["AE","UNITED ARAB EMIRATES"],["AF","AFGHANISTAN"],["AG","ANTIGUA AND BARBUDA"]]>>>printto_text(a)will output the following:┌──────┬──────────────────────┐│Code│Name│├──────┼──────────────────────┤│AD│ANDORRA│├──────┼──────────────────────┤│AE│UNITEDARABEMIRATES│├──────┼──────────────────────┤│AF│AFGHANISTAN│├──────┼──────────────────────┤│AG│ANTIGUAANDBARBUDA│└──────┴──────────────────────┘You can customize the output with optional arguments:>>>printto_text(a,header=True,corners="+",hor="-",ver="|",header_corners="+",header_hor="=",header_ver="!",formats=[">","<"])will output:+======+======================+!Code!Name!+======+======================+|AD|ANDORRA|+------+----------------------+|AE|UNITEDARABEMIRATES|+------+----------------------+|AF|AFGHANISTAN|+------+----------------------+|AG|ANTIGUAANDBARBUDA|+------+----------------------+|AI|ANGUILLA|+------+----------------------+See theDocumentationsection for more details about the optional arguments of
theto_textfunction.Command line utilityThe command line utility reads from its input the table, each line representing
a row, its entries being separated by\tcharacters (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 withtable--helpand closely follow
the optional arguments of the library’sto_textfunction as documentedhere.DocumentationThe optional arguments of theto_textfunction are as follows:ArgumentDefaultDescriptionformatsNoneFormat strings for the entries (see
below)padding(1, 1)Left and right padding lengthscorners"┌┬┐├┼┤└┴┘"Characters to use for the cornershor"─"Horizontal separation characterver"│"Vertical separation characterheaderFalseWether or not to display the first row
as a header rowheader_corners"╒╤╕╞╪╡"Characters to use for the header row
cornersheader_hor"═"Horizontal separation character for the
header rowheader_ver"│"Vertical separation character for the
header rowMore about some options:formatscan 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’sformat 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.cornersandheader_cornersare 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 history0.1 (2014-08-14)Initial release