Pretty-print tabular data in Python, a library and a command-line utility. Repository migrated from bitbucket.org/astanin/python-tabulate.

Overview

python-tabulate

Pretty-print tabular data in Python, a library and a command-line utility.

The main use cases of the library are:

  • printing small tables without hassle: just one function call, formatting is guided by the data itself
  • authoring tabular data for lightweight plain-text markup: multiple output formats suitable for further editing or transformation
  • readable presentation of mixed textual and numeric data: smart column alignment, configurable number formatting, alignment by a decimal point

Installation

To install the Python library and the command line utility, run:

pip install tabulate

The command line utility will be installed as tabulate to bin on Linux (e.g. /usr/bin); or as tabulate.exe to Scripts in your Python installation on Windows (e.g. C:\Python27\Scripts\tabulate.exe).

You may consider installing the library only for the current user:

pip install tabulate --user

In this case the command line utility will be installed to ~/.local/bin/tabulate on Linux and to %APPDATA%\Python\Scripts\tabulate.exe on Windows.

To install just the library on Unix-like operating systems:

TABULATE_INSTALL=lib-only pip install tabulate

On Windows:

set TABULATE_INSTALL=lib-only
pip install tabulate

Build status

Build status Build status

Library usage

The module provides just one function, tabulate, which takes a list of lists or another tabular data type as the first argument, and outputs a nicely formatted plain-text table:

>>> from tabulate import tabulate

>>> table = [["Sun",696000,1989100000],["Earth",6371,5973.6],
...          ["Moon",1737,73.5],["Mars",3390,641.85]]
>>> print(tabulate(table))
-----  ------  -------------
Sun    696000     1.9891e+09
Earth    6371  5973.6
Moon     1737    73.5
Mars     3390   641.85
-----  ------  -------------

The following tabular data types are supported:

  • list of lists or another iterable of iterables
  • list or another iterable of dicts (keys as columns)
  • dict of iterables (keys as columns)
  • two-dimensional NumPy array
  • NumPy record arrays (names as columns)
  • pandas.DataFrame

Examples in this file use Python2. Tabulate supports Python3 too.

Headers

The second optional argument named headers defines a list of column headers to be used:

>>> print(tabulate(table, headers=["Planet","R (km)", "mass (x 10^29 kg)"]))
Planet      R (km)    mass (x 10^29 kg)
--------  --------  -------------------
Sun         696000           1.9891e+09
Earth         6371        5973.6
Moon          1737          73.5
Mars          3390         641.85

If headers="firstrow", then the first row of data is used:

>>> print(tabulate([["Name","Age"],["Alice",24],["Bob",19]],
...                headers="firstrow"))
Name      Age
------  -----
Alice      24
Bob        19

If headers="keys", then the keys of a dictionary/dataframe, or column indices are used. It also works for NumPy record arrays and lists of dictionaries or named tuples:

>>> print(tabulate({"Name": ["Alice", "Bob"],
...                 "Age": [24, 19]}, headers="keys"))
  Age  Name
-----  ------
   24  Alice
   19  Bob

Row Indices

By default, only pandas.DataFrame tables have an additional column called row index. To add a similar column to any other type of table, pass showindex="always" or showindex=True argument to tabulate(). To suppress row indices for all types of data, pass showindex="never" or showindex=False. To add a custom row index column, pass showindex=rowIDs, where rowIDs is some iterable:

>>> print(tabulate([["F",24],["M",19]], showindex="always"))
-  -  --
0  F  24
1  M  19
-  -  --

Table format

There is more than one way to format a table in plain text. The third optional argument named tablefmt defines how the table is formatted.

Supported table formats are:

  • "plain"
  • "simple"
  • "github"
  • "grid"
  • "fancy_grid"
  • "pipe"
  • "orgtbl"
  • "jira"
  • "presto"
  • "pretty"
  • "psql"
  • "rst"
  • "mediawiki"
  • "moinmoin"
  • "youtrack"
  • "html"
  • "unsafehtml"
  • "latex"
  • "latex_raw"
  • "latex_booktabs"
  • "textile"

plain tables do not use any pseudo-graphics to draw lines:

>>> table = [["spam",42],["eggs",451],["bacon",0]]
>>> headers = ["item", "qty"]
>>> print(tabulate(table, headers, tablefmt="plain"))
item      qty
spam       42
eggs      451
bacon       0

simple is the default format (the default may change in future versions). It corresponds to simple_tables in Pandoc Markdown extensions:

>>> print(tabulate(table, headers, tablefmt="simple"))
item      qty
------  -----
spam       42
eggs      451
bacon       0

github follows the conventions of Github flavored Markdown. It corresponds to the pipe format without alignment colons:

>>> print(tabulate(table, headers, tablefmt="github"))
| item   | qty   |
|--------|-------|
| spam   | 42    |
| eggs   | 451   |
| bacon  | 0     |

grid is like tables formatted by Emacs' table.el package. It corresponds to grid_tables in Pandoc Markdown extensions:

>>> print(tabulate(table, headers, tablefmt="grid"))
+--------+-------+
| item   |   qty |
+========+=======+
| spam   |    42 |
+--------+-------+
| eggs   |   451 |
+--------+-------+
| bacon  |     0 |
+--------+-------+

fancy_grid draws a grid using box-drawing characters:

>>> print(tabulate(table, headers, tablefmt="fancy_grid"))
╒════════╤═══════╕
│ item   │   qty │
╞════════╪═══════╡
│ spam   │    42 │
├────────┼───────┤
│ eggs   │   451 │
├────────┼───────┤
│ bacon  │     0 │
╘════════╧═══════╛

presto is like tables formatted by Presto cli:

>>> print(tabulate(table, headers, tablefmt="presto"))
 item   |   qty
--------+-------
 spam   |    42
 eggs   |   451
 bacon  |     0

pretty attempts to be close to the format emitted by the PrettyTables library:

>>> print(tabulate(table, headers, tablefmt="pretty"))
+-------+-----+
| item  | qty |
+-------+-----+
| spam  | 42  |
| eggs  | 451 |
| bacon |  0  |
+-------+-----+

psql is like tables formatted by Postgres' psql cli:

>>> print(tabulate(table, headers, tablefmt="psql"))
+--------+-------+
| item   |   qty |
|--------+-------|
| spam   |    42 |
| eggs   |   451 |
| bacon  |     0 |
+--------+-------+

pipe follows the conventions of PHP Markdown Extra extension. It corresponds to pipe_tables in Pandoc. This format uses colons to indicate column alignment:

>>> print(tabulate(table, headers, tablefmt="pipe"))
| item   |   qty |
|:-------|------:|
| spam   |    42 |
| eggs   |   451 |
| bacon  |     0 |

orgtbl follows the conventions of Emacs org-mode, and is editable also in the minor orgtbl-mode. Hence its name:

>>> print(tabulate(table, headers, tablefmt="orgtbl"))
| item   |   qty |
|--------+-------|
| spam   |    42 |
| eggs   |   451 |
| bacon  |     0 |

jira follows the conventions of Atlassian Jira markup language:

>>> print(tabulate(table, headers, tablefmt="jira"))
|| item   ||   qty ||
| spam   |    42 |
| eggs   |   451 |
| bacon  |     0 |

rst formats data like a simple table of the reStructuredText format:

>>> print(tabulate(table, headers, tablefmt="rst"))
======  =====
item      qty
======  =====
spam       42
eggs      451
bacon       0
======  =====

mediawiki format produces a table markup used in Wikipedia and on other MediaWiki-based sites:

>>> print(tabulate(table, headers, tablefmt="mediawiki"))
{| class="wikitable" style="text-align: left;"
|+ <!-- caption -->
|-
! item   !! align="right"|   qty
|-
| spam   || align="right"|    42
|-
| eggs   || align="right"|   451
|-
| bacon  || align="right"|     0
|}

moinmoin format produces a table markup used in MoinMoin wikis:

>>> print(tabulate(table, headers, tablefmt="moinmoin"))
|| ''' item   ''' || ''' quantity   ''' ||
||  spam    ||  41.999      ||
||  eggs    ||  451         ||
||  bacon   ||              ||

youtrack format produces a table markup used in Youtrack tickets:

>>> print(tabulate(table, headers, tablefmt="youtrack"))
||  item    ||  quantity   ||
|   spam    |  41.999      |
|   eggs    |  451         |
|   bacon   |              |

textile format produces a table markup used in Textile format:

>>> print(tabulate(table, headers, tablefmt="textile"))
|_.  item   |_.   qty |
|<. spam    |>.    42 |
|<. eggs    |>.   451 |
|<. bacon   |>.     0 |

html produces standard HTML markup as an html.escape'd str with a .repr_html method so that Jupyter Lab and Notebook display the HTML and a .str property so that the raw HTML remains accessible. unsafehtml table format can be used if an unescaped HTML is required:

>>> print(tabulate(table, headers, tablefmt="html"))
<table>
<tbody>
<tr><th>item  </th><th style="text-align: right;">  qty</th></tr>
<tr><td>spam  </td><td style="text-align: right;">   42</td></tr>
<tr><td>eggs  </td><td style="text-align: right;">  451</td></tr>
<tr><td>bacon </td><td style="text-align: right;">    0</td></tr>
</tbody>
</table>

latex format creates a tabular environment for LaTeX markup, replacing special characters like _ or \ to their LaTeX correspondents:

>>> print(tabulate(table, headers, tablefmt="latex"))
\begin{tabular}{lr}
\hline
 item   &   qty \\
\hline
 spam   &    42 \\
 eggs   &   451 \\
 bacon  &     0 \\
\hline
\end{tabular}

latex_raw behaves like latex but does not escape LaTeX commands and special characters.

latex_booktabs creates a tabular environment for LaTeX markup using spacing and style from the booktabs package.

Column alignment

tabulate is smart about column alignment. It detects columns which contain only numbers, and aligns them by a decimal point (or flushes them to the right if they appear to be integers). Text columns are flushed to the left.

You can override the default alignment with numalign and stralign named arguments. Possible column alignments are: right, center, left, decimal (only for numbers), and None (to disable alignment).

Aligning by a decimal point works best when you need to compare numbers at a glance:

>>> print(tabulate([[1.2345],[123.45],[12.345],[12345],[1234.5]]))
----------
    1.2345
  123.45
   12.345
12345
 1234.5
----------

Compare this with a more common right alignment:

>>> print(tabulate([[1.2345],[123.45],[12.345],[12345],[1234.5]], numalign="right"))
------
1.2345
123.45
12.345
 12345
1234.5
------

For tabulate, anything which can be parsed as a number is a number. Even numbers represented as strings are aligned properly. This feature comes in handy when reading a mixed table of text and numbers from a file:

>>> import csv ; from StringIO import StringIO
>>> table = list(csv.reader(StringIO("spam, 42\neggs, 451\n")))
>>> table
[['spam', ' 42'], ['eggs', ' 451']]
>>> print(tabulate(table))
----  ----
spam    42
eggs   451
----  ----

To disable this feature use disable_numparse=True.

>>> print(tabulate.tabulate([["Ver1", "18.0"], ["Ver2","19.2"]], tablefmt="simple", disable_numparse=True))
----  ----
Ver1  18.0
Ver2  19.2
----  ----

Custom column alignment

tabulate allows a custom column alignment to override the above. The colalign argument can be a list or a tuple of stralign named arguments. Possible column alignments are: right, center, left, decimal (only for numbers), and None (to disable alignment). Omitting an alignment uses the default. For example:

>>> print(tabulate([["one", "two"], ["three", "four"]], colalign=("right",))
-----  ----
  one  two
three  four
-----  ----

Number formatting

tabulate allows to define custom number formatting applied to all columns of decimal numbers. Use floatfmt named argument:

>>> print(tabulate([["pi",3.141593],["e",2.718282]], floatfmt=".4f"))
--  ------
pi  3.1416
e   2.7183
--  ------

floatfmt argument can be a list or a tuple of format strings, one per column, in which case every column may have different number formatting:

>>> print(tabulate([[0.12345, 0.12345, 0.12345]], floatfmt=(".1f", ".3f")))
---  -----  -------
0.1  0.123  0.12345
---  -----  -------

Text formatting

By default, tabulate removes leading and trailing whitespace from text columns. To disable whitespace removal, set the global module-level flag PRESERVE_WHITESPACE:

import tabulate
tabulate.PRESERVE_WHITESPACE = True

Wide (fullwidth CJK) symbols

To properly align tables which contain wide characters (typically fullwidth glyphs from Chinese, Japanese or Korean languages), the user should install wcwidth library. To install it together with tabulate:

pip install tabulate[widechars]

Wide character support is enabled automatically if wcwidth library is already installed. To disable wide characters support without uninstalling wcwidth, set the global module-level flag WIDE_CHARS_MODE:

import tabulate
tabulate.WIDE_CHARS_MODE = False

Multiline cells

Most table formats support multiline cell text (text containing newline characters). The newline characters are honored as line break characters.

Multiline cells are supported for data rows and for header rows.

Further automatic line breaks are not inserted. Of course, some output formats such as latex or html handle automatic formatting of the cell content on their own, but for those that don't, the newline characters in the input cell text are the only means to break a line in cell text.

Note that some output formats (e.g. simple, or plain) do not represent row delimiters, so that the representation of multiline cells in such formats may be ambiguous to the reader.

The following examples of formatted output use the following table with a multiline cell, and headers with a multiline cell:

>>> table = [["eggs",451],["more\nspam",42]]
>>> headers = ["item\nname", "qty"]

plain tables:

>>> print(tabulate(table, headers, tablefmt="plain"))
item      qty
name
eggs      451
more       42
spam

simple tables:

>>> print(tabulate(table, headers, tablefmt="simple"))
item      qty
name
------  -----
eggs      451
more       42
spam

grid tables:

>>> print(tabulate(table, headers, tablefmt="grid"))
+--------+-------+
| item   |   qty |
| name   |       |
+========+=======+
| eggs   |   451 |
+--------+-------+
| more   |    42 |
| spam   |       |
+--------+-------+

fancy_grid tables:

>>> print(tabulate(table, headers, tablefmt="fancy_grid"))
╒════════╤═══════╕
│ item   │   qty │
│ name   │       │
╞════════╪═══════╡
│ eggs   │   451 │
├────────┼───────┤
│ more   │    42 │
│ spam   │       │
╘════════╧═══════╛

pipe tables:

>>> print(tabulate(table, headers, tablefmt="pipe"))
| item   |   qty |
| name   |       |
|:-------|------:|
| eggs   |   451 |
| more   |    42 |
| spam   |       |

orgtbl tables:

>>> print(tabulate(table, headers, tablefmt="orgtbl"))
| item   |   qty |
| name   |       |
|--------+-------|
| eggs   |   451 |
| more   |    42 |
| spam   |       |

jira tables:

>>> print(tabulate(table, headers, tablefmt="jira"))
| item   |   qty |
| name   |       |
|:-------|------:|
| eggs   |   451 |
| more   |    42 |
| spam   |       |

presto tables:

>>> print(tabulate(table, headers, tablefmt="presto"))
 item   |   qty
 name   |
--------+-------
 eggs   |   451
 more   |    42
 spam   |

pretty tables:

>>> print(tabulate(table, headers, tablefmt="pretty"))
+------+-----+
| item | qty |
| name |     |
+------+-----+
| eggs | 451 |
| more | 42  |
| spam |     |
+------+-----+

psql tables:

>>> print(tabulate(table, headers, tablefmt="psql"))
+--------+-------+
| item   |   qty |
| name   |       |
|--------+-------|
| eggs   |   451 |
| more   |    42 |
| spam   |       |
+--------+-------+

rst tables:

>>> print(tabulate(table, headers, tablefmt="rst"))
======  =====
item      qty
name
======  =====
eggs      451
more       42
spam
======  =====

Multiline cells are not well supported for the other table formats.

Usage of the command line utility

Usage: tabulate [options] [FILE ...]

FILE                      a filename of the file with tabular data;
                          if "-" or missing, read data from stdin.

Options:

-h, --help                show this message
-1, --header              use the first row of data as a table header
-o FILE, --output FILE    print table to FILE (default: stdout)
-s REGEXP, --sep REGEXP   use a custom column separator (default: whitespace)
-F FPFMT, --float FPFMT   floating point number format (default: g)
-f FMT, --format FMT      set output table format; supported formats:
                          plain, simple, github, grid, fancy_grid, pipe,
                          orgtbl, rst, mediawiki, html, latex, latex_raw,
                          latex_booktabs, tsv
                          (default: simple)

Performance considerations

Such features as decimal point alignment and trying to parse everything as a number imply that tabulate:

  • has to "guess" how to print a particular tabular data type
  • needs to keep the entire table in-memory
  • has to "transpose" the table twice
  • does much more work than it may appear

It may not be suitable for serializing really big tables (but who's going to do that, anyway?) or printing tables in performance sensitive applications. tabulate is about two orders of magnitude slower than simply joining lists of values with a tab, coma or other separator.

In the same time tabulate is comparable to other table pretty-printers. Given a 10x10 table (a list of lists) of mixed text and numeric data, tabulate appears to be slower than asciitable, and faster than PrettyTable and texttable The following mini-benchmark was run in Python 3.8.1 in Windows 10 x64:

===========================  ==========  ===========
Table formatter                time, μs    rel. time
===========================  ==========  ===========
csv to StringIO                    12.4          1.0
join with tabs and newlines        15.7          1.3
asciitable (0.8.0)                208.3         16.7
tabulate (0.8.7)                  492.1         39.5
PrettyTable (0.7.2)               945.5         76.0
texttable (1.6.2)                1239.5         99.6
===========================  ==========  ===========

Version history

The full version history can be found at the changelog.

How to contribute

Contributions should include tests and an explanation for the changes they propose. Documentation (examples, docstrings, README.md) should be updated accordingly.

This project uses nose testing framework and tox to automate testing in different environments. Add tests to one of the files in the test/ folder.

To run tests on all supported Python versions, make sure all Python interpreters, nose and tox are installed, then run tox in the root of the project source tree.

On Linux tox expects to find executables like python2.6, python2.7, python3.4 etc. On Windows it looks for C:\Python26\python.exe, C:\Python27\python.exe and C:\Python34\python.exe respectively.

To test only some Python environements, use -e option. For example, to test only against Python 2.7 and Python 3.6, run:

tox -e py27,py36

in the root of the project source tree.

To enable NumPy and Pandas tests, run:

tox -e py27-extra,py36-extra

(this may take a long time the first time, because NumPy and Pandas will have to be installed in the new virtual environments)

See tox.ini file to learn how to use nosetests directly to test individual Python versions.

Contributors

Sergey Astanin, Pau Tallada Crespí, Erwin Marsi, Mik Kocikowski, Bill Ryder, Zach Dwiel, Frederik Rietdijk, Philipp Bogensberger, Greg (anonymous), Stefan Tatschner, Emiel van Miltenburg, Brandon Bennett, Amjith Ramanujam, Jan Schulz, Simon Percivall, Javier Santacruz López-Cepero, Sam Denton, Alexey Ziyangirov, acaird, Cesar Sanchez, naught101, John Vandenberg, Zack Dever, Christian Clauss, Benjamin Maier, Andy MacKinlay, Thomas Roten, Jue Wang, Joe King, Samuel Phan, Nick Satterly, Daniel Robbins, Dmitry B, Lars Butler, Andreas Maier, Dick Marinus, Sébastien Celles, Yago González, Andrew Gaul, Wim Glenn, Jean Michel Rouly, Tim Gates, John Vandenberg, Sorin Sbarnea, Wes Turner, Andrew Tija, Marco Gorelli, Sean McGinnis, danja100.

Comments
  • ENH,BUG,SEC: Jupyter support, HTML escaping

    ENH,BUG,SEC: Jupyter support, HTML escaping

    I created a wrapper class to display HTML in JupyterLab; and then looked at the source and realized there's no cgi.escape / html.escape / bleach.clean / bleach.linkify; which (I assume) is an XSS vulnerability

    class TabulateHTML:
        def __init__(self, *args, **kwargs):
            kwargs['tablefmt'] = 'html'
            self.html = tabulate(*args, **kwargs)
        def _repr_html_(self):
            return self.html
    TabulateHTML(output)
    

    There's likely a better way to wrap TableFormat to return either an object with a .repr_html() method or an IPython.display.HTML when tablefmt='jupyterhtml' | 'jupyter' | 'ipython'?

    https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display

    https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.HTML

    (edit) Pull Requests:

    • [x] #26 BUG,SEC: html.escape to prevent XSS
    • [x] #27 ENH: return a wrapped str w/ a repr_html so that Jupyter displays the html
    opened by westurner 12
  • Wordwrap with long lines of text for a specific column

    Wordwrap with long lines of text for a specific column

    I have the following code:

    results = [('test', 'PR skipped', '428 A PR from test to develop is already open. Please close/merge it before you run this script. And some more text..  Skipping this module...')]
    headers=("Module Name", "Result", "Comment")
    print(tabulate(results,  headers, tablefmt="grid"))   
    

    and produces this

    image

    is there a way I can wrap text in the last column?

    enhancement good first issue 
    opened by koustubh25 10
  • Multi-line text within a cell does not format well with _outline formatting

    Multi-line text within a cell does not format well with _outline formatting

    I am working on printing output that sometimes have multiline entries in a cell. I like the *_outline tblfmt's and am wondering if the format can be adapted to fix formatting with multilines

    tabulate version 0.9.0

    Code

    import tabulate as tb
    from tabulate import tabulate
    print(tb.__version__)
    header = ['A', 'B', 'C', 'D']
    row1 = [
        '1', '2', '3', '4'
    ]
    
    row2 = [
        '5', '6', '7', '8\n88'
    ]
    
    row3 = [
        '9', '10', '11', '12'
    ]
    table_body = []
    table_body.append(row1)
    table_body.append(row2)
    table_body.append(row3)
    print(tabulate(table_body, headers=header))
    print(tabulate(table_body, headers=header, tablefmt='pretty'))
    print(tabulate(table_body, headers=header, tablefmt='simple_outline'))
    print(tabulate(table_body, headers=header, tablefmt='rounded_outline'))
    

    Output:

    0.9.0
      A    B    C  D
    ---  ---  ---  ---
      1    2    3  4
      5    6    7  8
                   88
      9   10   11  12
    +---+----+----+----+
    | A | B  | C  | D  |
    +---+----+----+----+
    | 1 | 2  | 3  | 4  |
    | 5 | 6  | 7  | 8  |
    |   |    |    | 88 |
    | 9 | 10 | 11 | 12 |
    +---+----+----+----+
    ┌─────┬─────┬─────┬──────┐
    │   A │   B │   C │ D    │
    ├─────┼─────┼─────┼──────┤
    │   1 │   2 │   3 │ 4    │
    │   5 │   6 │   7 │ 8
    88 │
    │   9 │  10 │  11 │ 12   │
    └─────┴─────┴─────┴──────┘
    ╭─────┬─────┬─────┬──────╮
    │   A │   B │   C │ D    │
    ├─────┼─────┼─────┼──────┤
    │   1 │   2 │   3 │ 4    │
    │   5 │   6 │   7 │ 8
    88 │
    │   9 │  10 │  11 │ 12   │
    ╰─────┴─────┴─────┴──────╯
    
    
    opened by networkprogrammer 9
  • Implements `{heavy,mixed}_{grid,outline}`

    Implements `{heavy,mixed}_{grid,outline}`

    Introduces, as suggested in #155, 4 new styles:

    • heavy_grid:

      ┏━━━━━━━━┳━━━━━━━┓
      ┃ item   ┃   qty ┃
      ┣━━━━━━━━╋━━━━━━━┫
      ┃ spam   ┃    42 ┃
      ┣━━━━━━━━╋━━━━━━━┫
      ┃ eggs   ┃   451 ┃
      ┣━━━━━━━━╋━━━━━━━┫
      ┃ bacon  ┃     0 ┃
      ┗━━━━━━━━┻━━━━━━━┛
      
    • mixed_grid:

      ┍━━━━━━━━┯━━━━━━━┑
      │ item   │   qty │
      ┝━━━━━━━━┿━━━━━━━┥
      │ spam   │    42 │
      ├────────┼───────┤
      │ eggs   │   451 │
      ├────────┼───────┤
      │ bacon  │     0 │
      ┕━━━━━━━━┷━━━━━━━┙
      
    • heavy_outline:

      ┏━━━━━━━━┳━━━━━━━┓
      ┃ item   ┃   qty ┃
      ┣━━━━━━━━╋━━━━━━━┫
      ┃ spam   ┃    42 ┃
      ┃ eggs   ┃   451 ┃
      ┃ bacon  ┃     0 ┃
      ┗━━━━━━━━┻━━━━━━━┛
      
    • mixed_outline:

      ┍━━━━━━━━┯━━━━━━━┑
      │ item   │   qty │
      ┝━━━━━━━━┿━━━━━━━┥
      │ spam   │    42 │
      │ eggs   │   451 │
      │ bacon  │     0 │
      ┕━━━━━━━━┷━━━━━━━┙
      

    Notes:

    Closes #155.

    opened by kdeldycke 9
  • Enable automatic word wrap based on desired maximum column width

    Enable automatic word wrap based on desired maximum column width

    This address as submitted issue #90 that allows automatically word wrapping long text string. From an API standpoint, this is automatically enabled by simply specifying the maximum column width for a given column. I selected max column width instead of just column width to remove any confusion where a user might think that specifying colwidth might be setting a guaranteed size, which is not the behavior here (though I could see it as a possible future update?)

    The "magic" here is a customized version of the textwrap.TextWrapper class to not use just len for strings and to also preserve ANSI color codes around wrapped lines. This is a non-trivial bit of code (even though heavily copy/pasted) with possible regression concerns, so I put it into a new test file to help tack this closely. Hopefully this fits within this projects idioms.

    To address the 'tests that need to be covered' from the issue discussion:

    • colwidths parameter can be specified as a number (the same for all column)
      • Done
    • colwidths parameter can be shorter than the number of column (don't wrap text in the remaining columns)
      • Done
    • the feature works when all columns are text
      • Done
    • the feature works when some columns are numeric
      • Done - I think. I think it behaves as sensibly as it can (ignores numbers), and it also works with disable_numparse. Let me know if you had something else in mind,
    • the feature works when some columns contain wide characters (Japanese/Chinese/Korean)
      • Done
    • the feature works when some columns contain invisible control characters (ASCII escape codes, color, hyperlinks)
      • Done
    • the feature works reasonably well or is disabled in formats which cannot support it (HTML? LaTeX?)
      • This has to be explicitly enabled by setting maxcolwidth, so I believe this meets the ability to ensure it does not impact these formats
    • when bidirectional script support is implemented first, this feature should work correctly when there's mixed left-to-right and right-to-left text in a cell (English + Arabic/Hebrew/...)
      • N/A

    Here's a small sample program I used to quickly demonstrate all the big capability updates

    from tabulate import tabulate
    all_data = [
        [('1', 'John Smith', '\033[31mThis is a rather long description that might look better if it is wrapped a bit\033[0m')],
        [('1', 'John Smith', '\033[31m약간 감싸면 더 잘 보일 수있는 다소 긴 설명입니다 설명입니다 설명입니다 설명입니다 설명\033[0m')],
        [('1', 'John Smith', '\033[31m\033[43mThis is a rather long description that might look better if it is wrapped a bit\033[0m')],
        [('1', 'John Smith', 'This is a rather \033[31mlong description\033[0m that might look better \033[93mif it is wrapped\033[0m a bit')]
    ]
    for data in all_data:
        headers=("Issue Id", "Author", "Description")
        print(tabulate(data,  headers, tablefmt="grid", maxcolwidths=[None, None, 30]))
    
    opened by magelisk 9
  • Seems a bug for the layout for the Chinese Character

    Seems a bug for the layout for the Chinese Character

    With Chinese characters in the DataFrame, it seems the tabulate package cannot identify the correct width.

    import pandas as pd
    import tabulate
    
    print(tabulate.__version__)
    print(tabulate.WIDE_CHARS_MODE)
    df = pd.DataFrame({
        '序号':['001', '021', '032'],
        '类型': ['汽车TESLA', '飞机737-MAX', 'TRAIN'],
        '备注': ['特殊', '一长串字符', '字符+1020']
    })
    print(tabulate.tabulate(df, tablefmt="github"))
    

    The Output is : 0.8.7 True |---|-----|-------------|------------| | 0 | 001 | 汽车TESLA | 特殊 | | 1 | 021 | 飞机737-MAX | 一长串字符 | | 2 | 032 | TRAIN | 字符+1020 | Screen Shot 2020-04-21 at 12 58 57 AM

    opened by sidkang 9
  • tabulate version 0.8.4 installation on Windows results in UnicodeDecodeError

    tabulate version 0.8.4 installation on Windows results in UnicodeDecodeError

    Hi, my team is seeing issues installing version 0.8.4 (it's a dependency of one of the other pypi packages we use).

    UnicodeEncodeError: 'charmap' codec can't encode characters in position 5907-5924: character maps to <undefined>

    (env) PS C:\Users\wchill\Projects> pip install tabulate
    Collecting tabulate
      Downloading https://files.pythonhosted.org/packages/76/35/ae65ed1268d6e2a1be141723e5fffdf4a28e4f4e7c1e083709b308998f90/tabulate-0.8.4.tar.gz (45kB)
         |████████████████████████████████| 51kB 1.1MB/s
        ERROR: Command errored out with exit status 1:
         command: 'c:\users\wchill\projects\env\scripts\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\WCHILL\\AppData\\Local\\Temp\\pip-install-u6zf4jij\\tabulate\\setup.py'"'"'; __file__='"'"'C:\\Users\\WCHILL\\AppData\\Local\\Temp\\pip-install-u6zf4jij\\tabulate\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base pip-egg-info
             cwd: C:\Users\WCHILL\AppData\Local\Temp\pip-install-u6zf4jij\tabulate\
        Complete output (7 lines):
        Traceback (most recent call last):
          File "<string>", line 1, in <module>
          File "C:\Users\WCHILL\AppData\Local\Temp\pip-install-u6zf4jij\tabulate\setup.py", line 25, in <module>
            f.write(LONG_DESCRIPTION)
          File "c:\users\wchill\projects\env\lib\encodings\cp1252.py", line 19, in encode
            return codecs.charmap_encode(input,self.errors,encoding_table)[0]
        UnicodeEncodeError: 'charmap' codec can't encode characters in position 5907-5924: character maps to <undefined>
        ----------------------------------------
    ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
    
    bug 
    opened by wchill 8
  • alignment error

    alignment error

    Hi,

    My code is like this:

    from tabulate import tabulate
    
    
    res = [{'object': '1986', 'relation': '出生日期', 'subject': '爱德华·尼科·埃尔南迪斯'}, {'object': '哥伦比亚', 'relation': '国籍', 'subject': '爱德华·尼科·埃尔南迪斯'}, {'object': '70公分', 'relation': '身高', 'subject': '爱德华·尼科·埃尔南迪斯'}]
    
    title = ['subject', 'relation', 'object']
    tbl = [title] + [[el['subject'], el['relation'], el['object']] for el in res]
    tbl = tabulate(tbl, headers='firstrow', tablefmt='orgtbl', colalign=('right',))
    
    print(tbl)
    

    And the result is:

    |      subject | relation   | object   |
    |--------------+------------+----------|
    | 爱德华·尼科·埃尔南迪斯 | 出生日期       | 1986     |
    | 爱德华·尼科·埃尔南迪斯 | 国籍         | 哥伦比亚     |
    | 爱德华·尼科·埃尔南迪斯 | 身高         | 70公分     |
    

    How could I make the column aligns well ?

    opened by CoinCheung 7
  • Int64 numbers from Pandas DataFrames interpreted as float and incorrectly printed due to overflow

    Int64 numbers from Pandas DataFrames interpreted as float and incorrectly printed due to overflow

    Issue

    When Tabulate prints a Pandas DataFrame with an int64 field the resulting value is incorrectly shown due to overflow. It appears that Tabulate is interpreting Pandas int64 fields as float and then performing a format() call which fails natively in Python:

    format(503498111827123021, '.0f')
    '503498111827123008'
    

    Expected Behavior

    This error does not happen when passing a 64 bit int as a list directly into Tabulate because it is treating the int64 as an int. I believe the fix here is that Tabulate should recognize that a DataFrame int64 field should also be treated as an integer and not attempt to perform a floating format operation.

    Reproduction

    Test 64bit int with Pandas DataFrame head() -> Correct

    import pandas as pd
    
    df = pd.DataFrame({'colA': [503498111827123021]})
    df.dtypes
    
    colA    int64
    dtype: object
    
    df.head()
                     colA
    0  503498111827123021
    

    Test 64bit int withtabulate() on list data-> Correct

    from tabulate import tabulate
    
    table = [[503498111827123021]]
    print(tabulate(table))
    ------------------
    503498111827123021
    ------------------
    
    print(tabulate(table, floatfmt='.0f'))
    ------------------
    503498111827123021
    ------------------
    

    Test 64 bit float with with tabulate() on float data -> Incorrect

    from tabulate import tabulate
    
    table = [[503498111827123021.0]]
    print(tabulate(table, floatfmt='.0f'))
    ------------------
    503498111827123008
    ------------------
    

    Test 64 bit int DataFrame field with various combinations -> Incorrect

    from tabulate import tabulate
    import pandas as pd
    
    df = pd.DataFrame({'colA': [503498111827123021]})
    print(tabulate(df, floatfmt='.0f'))
    -  ------------------
    0  503498111827123008
    -  ------------------
    
    print(tabulate(df))
    # Without arguments this is being seen as float
    -  -----------
    0  5.03498e+17
    -  -----------
    
    print(df.to_markdown(floatfmt='.0f'))
    |    |               colA |
    |---:|-------------------:|
    |  0 | 503498111827123008 |
    
    print(df.to_markdown())
    |    |        colA |
    |---:|------------:|
    |  0 | 5.03498e+17 |
    
    bug duplicate 
    opened by jbencina 6
  • Add new `rounded`/`simple`/`double`_`grid`/`outline` formats

    Add new `rounded`/`simple`/`double`_`grid`/`outline` formats

    • Add new simple_grid format, documentation and tests:

      ┌────────┬───────┐
      │ item   │   qty │
      ├────────┼───────┤
      │ spam   │    42 │
      ├────────┼───────┤
      │ eggs   │   451 │
      ├────────┼───────┤
      │ bacon  │     0 │
      └────────┴───────┘
      
    • Add new rounded_grid format, documentation and tests:

      ╭────────┬───────╮
      │ item   │   qty │
      ├────────┼───────┤
      │ spam   │    42 │
      ├────────┼───────┤
      │ eggs   │   451 │
      ├────────┼───────┤
      │ bacon  │     0 │
      ╰────────┴───────╯
      
    • Add new double_grid format, documentation and tests:

      ╔════════╦═══════╗
      ║ item   ║   qty ║
      ╠════════╬═══════╣
      ║ spam   ║    42 ║
      ╠════════╬═══════╣
      ║ eggs   ║   451 ║
      ╠════════╬═══════╣
      ║ bacon  ║     0 ║
      ╚════════╩═══════╝
      
    • Add new outline format, documentation and tests:

      +--------+-------+
      | item   |   qty |
      +========+=======+
      | spam   |    42 |
      | eggs   |   451 |
      | bacon  |     0 |
      +--------+-------+
      
    • Add new simple_outline format, documentation and tests:

      ┌────────┬───────┐
      │ item   │   qty │
      ├────────┼───────┤
      │ spam   │    42 │
      │ eggs   │   451 │
      │ bacon  │     0 │
      └────────┴───────┘
      
    • Add new rounded_outline format, documentation and tests:

      ╭────────┬───────╮
      │ item   │   qty │
      ├────────┼───────┤
      │ spam   │    42 │
      │ eggs   │   451 │
      │ bacon  │     0 │
      ╰────────┴───────╯
      
    • Add new double_outline format, documentation and tests:

      ╔════════╦═══════╗
      ║ item   ║   qty ║
      ╠════════╬═══════╣
      ║ spam   ║    42 ║
      ║ eggs   ║   451 ║
      ║ bacon  ║     0 ║
      ╚════════╩═══════╝
      
    • Add missing fancy_outline documentation (refs #80, closes #146).

    • Add missing fancy_outline tests (refs #80, closes #146).

    opened by kdeldycke 6
  • {heavy,simple}_outline formats do not handle mulitline values correctly

    {heavy,simple}_outline formats do not handle mulitline values correctly

    Wrong:

    >>> print(tabulate([["a\nbb", "a\nbb\nccc\ndddd"]], rowalign="center", tablefmt="heavy_outline"))
    ┏━┳━┓
    ┃ a
    bb  ┃ a
    bb
    ccc
    dddd  ┃
    ┗━┻━┛
    

    Correct:

    >>> print(tabulate([["a\nbb", "a\nbb\nccc\ndddd"]], rowalign="center", tablefmt="grid"))
    +----+------+
    |    | a    |
    | a  | bb   |
    | bb | ccc  |
    |    | dddd |
    +----+------+
    

    heavy_grid works fine too:

    >>> print(tabulate([["a\nbb", "a\nbb\nccc\ndddd"]], rowalign="center", tablefmt="heavy_grid"))
    ┏━━━━┳━━━━━━┓
    ┃    ┃ a    ┃
    ┃ a  ┃ bb   ┃
    ┃ bb ┃ ccc  ┃
    ┃    ┃ dddd ┃
    ┗━━━━┻━━━━━━┛
    

    @kdeldycke You have implemented these formats. Would you mind to take a look?

    opened by astanin 5
  • Header at bottom

    Header at bottom

    Is there an option to print the header at the bottom (which would make it a footer)?

    The use case is printing a very tall table in a terminal. The user would have to scroll up to see the header, but if it could be added to the bottom, this is not necessary. As an example, Wikipedia often has top and bottom headers on its pages.

    opened by metov 0
  • Fix #135. Get rid of exceptions when converting values to numbers.

    Fix #135. Get rid of exceptions when converting values to numbers.

    Fix #135

    • [x] Add the test to check for exceptions
    • [x] Fix for booleans to float conversion
    • [x] Fix for booleans and strings formatted as integers

    Note: there is (and has been for a while as I understand it) an inconsistency in boolean representation in the columns which contain a mix of bools and numbers. Booleans were passed as text in case of unformatted integer column type, and converted into [0, 1] otherwise. For example:

    >>> print(tabulate([[1.1, 1], [True, True]]))
    ---  ----
    1.1     1
    1    True
    ---  ----
    

    This behavior is PRESERVED in the current fix, no breaking changes introduced, just "True" and True are handled in the same way now.

    This pull request is still a draft because I found another issue which would possibly be wise to fix here since it also concerns the _format function.

    opened by ilya112358 0
  • Fix #207. Update README: headers for dictionaries.

    Fix #207. Update README: headers for dictionaries.

    Fix #207

    • Change usage example to reconcile with the insertion-order preservation of dictionaries
    • Document a long-existing feature of using a dictionary as the header for a list of dictionaries
    opened by ilya112358 0
  • Fix #124. Improve error reporting when non-iterable data provided.

    Fix #124. Improve error reporting when non-iterable data provided.

    Fix #124. Also related to #208 and #191.

    This is to provide an additional message to the user when the program throws a ... is not iterable exception.

    Two blocks of try-except added: one for the case when a non-iterable is passed and another when an iterable of non-iterables is passed.

    Typical output:

    >>> print(tabulate({"a": 1, "b": 2}))
    Traceback (most recent call last):
      File "C:\Users\Ibo\Python\python-tabulate\tabulate\__init__.py", line 1373, in _normalize_tabular_data
        izip_longest(*tabular_data.values())
    TypeError: 'int' object is not iterable
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Users\Ibo\Python\python-tabulate\tabulate\__init__.py", line 2080, in tabulate
        list_of_lists, headers = _normalize_tabular_data(
                                 ^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\Ibo\Python\python-tabulate\tabulate\__init__.py", line 1376, in _normalize_tabular_data
        raise TypeError(err_msg)
    TypeError:
    
    To build a table python-tabulate requires two-dimensional data like a list of lists or similar.
    Did you forget a pair of extra [] or ',' in ()?
    >>>
    
    opened by ilya112358 0
  • Fix bug/issue #231: SEPARATING_LINE feature doesn't work when the requested format pads columns

    Fix bug/issue #231: SEPARATING_LINE feature doesn't work when the requested format pads columns

    The subject says it all.. 😉 Tested:

    lint: OK (6.63=setup[4.07]+cmd[2.56] seconds)
      py37: OK (5.15=setup[3.97]+cmd[1.19] seconds)
      py38: OK (4.83=setup[3.63]+cmd[1.20] seconds)
      py39: OK (4.42=setup[3.28]+cmd[1.14] seconds)
      py310: OK (4.77=setup[3.57]+cmd[1.20] seconds)
      py311: OK (4.59=setup[3.34]+cmd[1.25] seconds)
      congratulations :) (30.46 seconds)
    
    opened by jeromegit 0
Pretty and useful exceptions in Python, automatically.

better-exceptions Pretty and more helpful exceptions in Python, automatically. Usage Install better_exceptions via pip: $ pip install better_exception

Qix 4.3k Dec 29, 2022
Display tabular data in a visually appealing ASCII table format

PrettyTable Installation Install via pip: python -m pip install -U prettytable Install latest development version: python -m pip install -U git+https

Jazzband 924 Jan 5, 2023
Multi-processing capable print-like logger for Python

MPLogger Multi-processing capable print-like logger for Python Requirements and Installation Python 3.8+ is required Pip pip install mplogger Manual P

Eötvös Loránd University Department of Digital Humanities 1 Jan 28, 2022
Command-line tool that instantly fetches Stack Overflow results when an exception is thrown

rebound Rebound is a command-line tool that instantly fetches Stack Overflow results when an exception is thrown. Just use the rebound command to exec

Jonathan Shobrook 3.9k Jan 3, 2023
Lazy Profiler is a simple utility to collect CPU, GPU, RAM and GPU Memory stats while the program is running.

lazyprofiler Lazy Profiler is a simple utility to collect CPU, GPU, RAM and GPU Memory stats while the program is running. Installation Use the packag

Shankar Rao Pandala 28 Dec 9, 2022
dash-manufacture-spc-dashboard is a dashboard for monitoring read-time process quality along manufacture production line

In our solution based on plotly, dash and influxdb, the user will firstly generate the specifications for different robots, and then a wide range of interactive visualizations for different machines for machine power, machine cost, and total cost based on the energy time and total energy getting dynamically from sensors. If a threshold is met, the alert email is generated for further operation.

Dequn Teng 1 Feb 13, 2022
Soda SQL Data testing, monitoring and profiling for SQL accessible data.

Soda SQL Data testing, monitoring and profiling for SQL accessible data. What does Soda SQL do? Soda SQL allows you to Stop your pipeline when bad dat

Soda Data Monitoring 51 Jan 1, 2023
Rich is a Python library for rich text and beautiful formatting in the terminal.

Rich 中文 readme • lengua española readme • Läs på svenska Rich is a Python library for rich text and beautiful formatting in the terminal. The Rich API

Will McGugan 41.5k Jan 7, 2023
Simple and versatile logging library for python 3.6 above

Simple and versatile logging library for python 3.6 above

Miguel 1 Nov 23, 2022
Log processor for nginx or apache that extracts user and user sessions and calculates other types of useful data for bot detection or traffic analysis

Log processor for nginx or apache that extracts user and user sessions and calculates other types of useful data for bot detection or traffic analysis

David Puerta Martín 1 Nov 11, 2021
A basic logging library for Python.

log.py ?? About: A basic logging library for Python with the capability to: save to files. have custom formats. have custom levels. be used instantiat

Sebastiaan Bij 1 Jan 19, 2022
A python library used to interact with webots robocup game web logs

A python library used to interact with webots robocup game web logs

Hamburg Bit-Bots 2 Nov 5, 2021
A lightweight logging library for python applications

cakelog a lightweight logging library for python applications This is a very small logging library to make logging in python easy and simple. config o

null 2 Jan 5, 2022
A Python library that tees the standard output & standard error from the current process to files on disk, while preserving terminal semantics

A Python library that tees the standard output & standard error from the current process to files on disk, while preserving terminal semantics (so breakpoint(), etc work as normal)

Greg Brockman 7 Nov 30, 2022
pyEventLogger - a simple Python Library for making customized Logs of certain events that occur in a program

pyEventLogger is a simple Python Library for making customized Logs of certain events that occur in a program. The logs can be fully customized and can be printed in colored format or can be stored in a file.

Siddhesh Chavan 2 Nov 3, 2022
Outlog it's a library to make logging a simple task

outlog Outlog it's a library to make logging a simple task!. I'm a lazy python user, the times that i do logging on my apps it's hard to do, a lot of

ZSendokame 2 Mar 5, 2022
metovlogs is a very simple logging library

metovlogs is a very simple logging library. Setup is one line, then you can use it as a drop-in print replacement. Sane and useful log format out of the box. Best for small or early projects.

Azat Akhmetov 1 Mar 1, 2022
ClusterMonitor - a very simple python script which monitors and records the CPU and RAM consumption of submitted cluster jobs

ClusterMonitor A very simple python script which monitors and records the CPU and RAM consumption of submitted cluster jobs. Usage To start recording

null 23 Oct 4, 2021
Robust and effective logging for Python 2 and 3.

Robust and effective logging for Python 2 and 3.

Chris Hager 1k Jan 4, 2023