GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing pypi.org — tabulate tabulate 0.10.0Pretty-print tabular data pip install tabulate python-tabulatePretty-print tabular data in Python, a library and a command-line utility. The main use cases of the library are:
InstallationTo install the Python library and the command line utility, run: pip install tabulate
The command line utility will be installed as 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
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 statusLibrary usageThe module provides just one function, >>> 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:
Tabulate is a Python3 library. HeadersThe second optional argument named >>> 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 >>> print(tabulate([["Name","Age"],["Alice",24],["Bob",19]],
... headers="firstrow"))
Name Age
------ -----
Alice 24
Bob 19
If >>> print(tabulate({"Name": ["Alice", "Bob"],
... "Age": [24, 19]}, headers="keys"))
Name Age
------ -----
Alice 24
Bob 19
When data is a list of dictionaries, a dictionary can be passed as >>> print(tabulate([{1: "Alice", 2: 24}, {1: "Bob", 2: 19}],
... headers={1: "Name", 2: "Age"}))
Name Age
------ -----
Alice 24
Bob 19
Row IndicesBy default, only pandas.DataFrame tables have an additional column
called row index. To add a similar column to any other type of table,
pass >>> print(tabulate([["F",24],["M",19]], showindex="always"))
- - --
0 F 24
1 M 19
- - --
Table formatThere is more than one way to format a table in plain text. The third
optional argument named Supported table formats are:
>>> table = [["spam",42],["eggs",451],["bacon",0]]
>>> headers = ["item", "qty"]
>>> print(tabulate(table, headers, tablefmt="plain"))
item qty
spam 42
eggs 451
bacon 0
>>> print(tabulate(table, headers, tablefmt="simple"))
item qty
------ -----
spam 42
eggs 451
bacon 0
>>> print(tabulate(table, headers, tablefmt="github"))
| item | qty |
|--------|-------|
| spam | 42 |
| eggs | 451 |
| bacon | 0 |
>>> print(tabulate(table, headers, tablefmt="grid"))
+--------+-------+
| item | qty |
+========+=======+
| spam | 42 |
+--------+-------+
| eggs | 451 |
+--------+-------+
| bacon | 0 |
+--------+-------+
>>> print(tabulate(table, headers, tablefmt="fancy_grid"))
╒════════╤═══════╕
│ item │ qty │
╞════════╪═══════╡
│ spam │ 42 │
├────────┼───────┤
│ eggs │ 451 │
├────────┼───────┤
│ bacon │ 0 │
╘════════╧═══════╛
>>> print(tabulate(table, headers, tablefmt="presto"))
item | qty
--------+-------
spam | 42
eggs | 451
bacon | 0
>>> print(tabulate(table, headers, tablefmt="pretty"))
+-------+-----+
| item | qty |
+-------+-----+
| spam | 42 |
| eggs | 451 |
| bacon | 0 |
+-------+-----+
>>> print(tabulate(table, headers, tablefmt="psql"))
+--------+-------+
| item | qty |
|--------+-------|
| spam | 42 |
| eggs | 451 |
| bacon | 0 |
+--------+-------+
>>> print(tabulate(table, headers, tablefmt="pipe"))
| item | qty |
|:-------|------:|
| spam | 42 |
| eggs | 451 |
| bacon | 0 |
>>> print(tabulate(table, headers, tablefmt="asciidoc"))
[cols="<8,>7",options="header"]
|====
| item | qty
| spam | 42
| eggs | 451
| bacon | 0
|====
>>> print(tabulate(table, headers, tablefmt="orgtbl"))
| item | qty |
|--------+-------|
| spam | 42 |
| eggs | 451 |
| bacon | 0 |
>>> print(tabulate(table, headers, tablefmt="jira"))
|| item || qty ||
| spam | 42 |
| eggs | 451 |
| bacon | 0 |
>>> print(tabulate(table, headers, tablefmt="rst"))
====== =====
item qty
====== =====
spam 42
eggs 451
bacon 0
====== =====
>>> print(tabulate(table, headers, tablefmt="mediawiki"))
{| class="wikitable" style="text-align: left;"
|+ <!-- caption -->
|-
! item !! style="text-align: right;"| qty
|-
| spam || style="text-align: right;"| 42
|-
| eggs || style="text-align: right;"| 451
|-
| bacon || style="text-align: right;"| 0
|}
>>> print(tabulate(table, headers, tablefmt="moinmoin"))
|| ''' item ''' ||<style="text-align: right;"> ''' qty ''' ||
|| spam ||<style="text-align: right;"> 42 ||
|| eggs ||<style="text-align: right;"> 451 ||
|| bacon ||<style="text-align: right;"> 0 ||
>>> print(tabulate(table, headers, tablefmt="youtrack"))
|| item || qty ||
| spam | 42 |
| eggs | 451 |
| bacon | 0 |
>>> print(tabulate(table, headers, tablefmt="textile"))
|_. item |_. qty |
|<. spam |>. 42 |
|<. eggs |>. 451 |
|<. bacon |>. 0 |
>>> print(tabulate(table, headers, tablefmt="html"))
<table>
<thead>
<tr><th>item </th><th style="text-align: right;"> qty</th></tr>
</thead>
<tbody>
<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>
>>> print(tabulate(table, headers, tablefmt="latex"))
\begin{tabular}{lr}
\hline
item & qty \\
\hline
spam & 42 \\
eggs & 451 \\
bacon & 0 \\
\hline
\end{tabular}
Column alignment
You can override the default alignment with 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 >>> import csv; from io 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 >>> print(tabulate([["Ver1", "18.0"], ["Ver2","19.2"]], tablefmt="simple", disable_numparse=True))
---- ----
Ver1 18.0
Ver2 19.2
---- ----
Custom column alignment
>>> print(tabulate([[1,2,3,4],[111,222,333,444]], colglobalalign='center', colalign = ('global','left','right')))
--- --- --- ---
1 2 3 4
111 222 333 444
--- --- --- ---
Custom header alignmentHeaders' alignment can be defined separately from columns'. Like for columns, you can use:
>>> print(tabulate([[1,2,3,4,5,6],[111,222,333,444,555,666]], colglobalalign = 'center', colalign = ('left',), headers = ['h','e','a','d','e','r'], headersglobalalign = 'right', headersalign = ('same','same','left','global','center')))
h e a d e r
--- --- --- --- --- ---
1 2 3 4 5 6
111 222 333 444 555 666
Number formatting
>>> print(tabulate([["pi",3.141593],["e",2.718282]], floatfmt=".4f"))
-- ------
pi 3.1416
e 2.7183
-- ------
>>> print(tabulate([[0.12345, 0.12345, 0.12345]], floatfmt=(".1f", ".3f")))
--- ----- -------
0.1 0.123 0.12345
--- ----- -------
Type Deduction and Missing ValuesWhen >>> from fractions import Fraction
>>> test_table = [
... [None, "1.23423515351", Fraction(1, 3)],
... [Fraction(56789, 1000000), 12345.1, b"abc"],
... ["", b"", None],
... [Fraction(10000, 3), None, ""],
... ]
>>> print(tabulate(test_table, floatfmt=",.5g", missingval="?"))
------------ ----------- ---
? 1.2342 1/3
0.056789 12,345 abc
?
3,333.3 ?
------------ ----------- ---
The deduced type (eg. str, float) influences the rendering of any types
that have alternative representations. For example, since Therefore, if your table contains types convertible to int/float but you'd
prefer they be represented as strings, or your strings might all look
like numbers such as "1e23": either convert them to the desired
representation before you Text formattingBy default, Wide (fullwidth CJK) symbolsTo properly align tables which contain wide characters (typically
fullwidth glyphs from Chinese, Japanese or Korean languages), the user
should install pip install tabulate[widechars]
Wide character support is enabled automatically if import tabulate
tabulate.WIDE_CHARS_MODE = False
Multiline cellsMost 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"]
>>> print(tabulate(table, headers, tablefmt="plain"))
item qty
name
eggs 451
more 42
spam
>>> print(tabulate(table, headers, tablefmt="simple"))
item qty
name
------ -----
eggs 451
more 42
spam
>>> print(tabulate(table, headers, tablefmt="grid"))
+--------+-------+
| item | qty |
| name | |
+========+=======+
| eggs | 451 |
+--------+-------+
| more | 42 |
| spam | |
+--------+-------+
>>> print(tabulate(table, headers, tablefmt="fancy_grid"))
╒════════╤═══════╕
│ item │ qty │
│ name │ │
╞════════╪═══════╡
│ eggs │ 451 │
├────────┼───────┤
│ more │ 42 │
│ spam │ │
╘════════╧═══════╛
>>> print(tabulate(table, headers, tablefmt="pipe"))
| item | qty |
| name | |
|:-------|------:|
| eggs | 451 |
| more | 42 |
| spam | |
>>> print(tabulate(table, headers, tablefmt="orgtbl"))
| item | qty |
| name | |
|--------+-------|
| eggs | 451 |
| more | 42 |
| spam | |
>>> print(tabulate(table, headers, tablefmt="jira"))
|| item || qty ||
|| name || ||
| eggs | 451 |
| more | 42 |
| spam | |
>>> print(tabulate(table, headers, tablefmt="presto"))
item | qty
name |
--------+-------
eggs | 451
more | 42
spam |
>>> print(tabulate(table, headers, tablefmt="pretty"))
+------+-----+
| item | qty |
| name | |
+------+-----+
| eggs | 451 |
| more | 42 |
| spam | |
+------+-----+
>>> print(tabulate(table, headers, tablefmt="psql"))
+--------+-------+
| item | qty |
| name | |
|--------+-------|
| eggs | 451 |
| more | 42 |
| spam | |
+--------+-------+
>>> 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. Automating MultilinesWhile tabulate supports data passed in with multilines entries explicitly provided, it also provides some support to help manage this work internally. The Use The wrapping uses the python standard textwrap.wrap function with default parameters - aside from width. This example demonstrates usage of automatic multiline wrapping, though typically the lines being wrapped would probably be significantly longer than this. >>> print(tabulate([["John Smith", "Middle Manager"]], headers=["Name", "Title"], tablefmt="grid", maxcolwidths=[None, 8]))
+------------+---------+
| Name | Title |
+============+=========+
| John Smith | Middle |
| | Manager |
+------------+---------+
Text is preferably wrapped on whitespaces and right after the hyphens in hyphenated words. break_long_words (default: True) If true, then words longer than width will be broken in order to ensure that no lines are longer than width. If it is false, long words will not be broken, and some lines may be longer than width. (Long words will be put on a line by themselves, in order to minimize the amount by which width is exceeded.) break_on_hyphens (default: True) If true, wrapping will occur preferably on whitespaces and right after hyphens in compound words, as it is customary in English. If false, only whitespaces will be considered as potentially good places for line breaks. >>> print(tabulate([["John Smith", "Middle-Manager"]], headers=["Name", "Title"], tablefmt="grid", maxcolwidths=[None, 5], break_long_words=False))
+------------+---------+
| Name | Title |
+============+=========+
| John Smith | Middle- |
| | Manager |
+------------+---------+
>>> print(tabulate([["John Smith", "Middle-Manager"]], headers=["Name", "Title"], tablefmt="grid", maxcolwidths=[None, 5], break_long_words=False, break_on_hyphens=False))
+------------+----------------+
| Name | Title |
+============+================+
| John Smith | Middle-Manager |
+------------+----------------+
Adding Separating linesOne might want to add one or more separating lines to highlight different sections in a table. The separating lines will be of the same type as the one defined by the specified formatter as either the linebetweenrows, linebelowheader, linebelow, lineabove or just a simple empty line when none is defined for the formatter
ANSI supportANSI escape codes are non-printable byte sequences usually used for terminal operations like setting color output or modifying cursor positions. Because multi-byte ANSI sequences are inherently non-printable, they can still introduce unwanted extra length to strings. For example:
To deal with this, string lengths are calculated after first removing all ANSI escape sequences. This ensures that the actual printable length is used for column widths, rather than the byte length. In the final, printable table, however, ANSI escape sequences are not removed so the original styling is preserved. Some terminals support a special grouping of ANSI escape sequences that are intended to display hyperlinks much in the same way they are shown in browsers. These are handled just as mentioned before: non-printable ANSI escape sequences are removed prior to string length calculation. The only difference with escaped hyperlinks is that column width will be based on the length of the URL text rather than the URL itself (terminals would show this text). For example:
Usage of the command line utility
Performance considerationsSuch features as decimal point alignment and trying to parse everything
as a number imply that
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. At the same time,
Version historyThe full version history can be found at the changelog. How to contributeContributions should include tests and an explanation for the changes they propose. Documentation (examples, docstrings, README.md) should be updated accordingly. This project uses pytest testing
framework and tox to automate testing in
different environments. Add tests to one of the files in the To run tests on all supported Python versions, make sure all Python
interpreters, On Linux One way to install all the required versions of the Python interpreter is to use pyenv. All versions can then be easily installed with something like:
Don't forget to change your
To test only some Python environments, use tox -e py311,py312
in the root of the project source tree. To enable NumPy and Pandas tests, run: tox -e py311-extra,py312-extra
(this may take a long time the first time, because NumPy and Pandas will have to be installed in the new virtual environments) To fix code formatting: tox -e lint
See To test the "doctest" examples and their outputs in python3 -m pip install pytest-doctestplus[md]
python3 -m doctest README.md
ContributorsSergey 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, endolith, Dominic Davis-Foster, pavlocat, Daniel Aslau, paulc, Felix Yan, Shane Loretz, Frank Busse, Harsh Singh, Derek Weitzel, Vladimir Vrzić, 서승우 (chrd5273), Georgy Frolov, Christian Cwienk, Bart Broere, Vilhelm Prytz, Alexander Gažo, Hugo van Kemenade, jamescooke, Matt Warner, Jérôme Provensal, Michał Górny, Kevin Deldycke, Kian-Meng Ang, Kevin Patterson, Shodhan Save, cleoold, KOLANICH, Vijaya Krishna Kasula, Furcy Pin, Christian Fibich, Shaun Duncan, Dimitri Papadopoulos, Élie Goudout, Racerroar888, Phill Zarfos, Keyacom, Andrew Coffey, Arpit Jain, Israel Roldan, ilya112358, Dan Nicholson, Frederik Scheerer, cdar07 (cdar), Racerroar888, Perry Kundert. Project linksKey datesPyPI dataData sourced directly from PyPI's database.
1 maintainerPyPI dataData sourced directly from PyPI's database.
CreditsAuthor: Sergey Astanin RequiresPython >=3.10 Provides Extra
Classifiers
MetadataProject linksKey datesPyPI dataData sourced directly from PyPI's database.
1 maintainerPyPI dataData sourced directly from PyPI's database.
CreditsAuthor: Sergey Astanin RequiresPython >=3.10 Provides Extra
Classifiers
Release files for tabulate 0.10.0For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation. Source distribution (sdist)
Built distribution (wheel)
Total release size: 131.6 kB Release files / tabulate-0.10.0.tar.gz
Release files / tabulate-0.10.0-py3-none-any.whl
Security for tabulate 0.10.0ProvenanceProvenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats. No provenance foundNone of the files in this release were uploaded with provenance metadata. A note on package securityPyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package. To learn more about protecting yourself from malicious actors, visit our package security documentation. Release history Release notifications | RSS feed | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |