by shigemk2

当面は技術的なことしか書かない

python-tabulate

なんかよくわからないなgithub/asciiで改行がおかしくなるのはマークダウンテーブルで改行が観念できないからだろうか。

>>> from tabulate import tabulate
>>> table = [["Sun",696000,1989100000],["Earth",6371,5973.6],
...     ["Moon",1737,73.5],["Mars",3390,641.85]]
>>> table
[['Sun', 696000, 1989100000], ['Earth', 6371, 5973.6], ['Moon', 1737, 73.5], ['Mars', 3390, 641.85]]
>>> 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
>>> table = [["spam",42],["eggs",451],["bacon",0]]
>>> headers = ["item", "qty"]
>>> print(tabulate(table, headers, tablefmt="plain"))
item      qty
spam       42
eggs      451
bacon       0
>>> table = [["spam",'1\n2'],["eggs",451],["bacon",0]]
>>> print(tabulate(table, headers, tablefmt="plain"))
item    qty
spam    1
        2
eggs    451
bacon   0
>>> print(tabulate(table, headers, tablefmt="simple"))
item    qty
------  -----
spam    1
        2
eggs    451
bacon   0
>>> print(tabulate(table, headers, tablefmt="ascii"))
item    qty
------  -----
spam    1
2
eggs    451
bacon   0
>>> print(tabulate(table, headers, tablefmt="github"))
| item   | qty   |
|--------|-------|
| spam   | 1
2   |
| eggs   | 451   |
| bacon  | 0     |

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