METADATA 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. Metadata-Version: 2.1
  2. Name: tabulate
  3. Version: 0.9.0
  4. Summary: Pretty-print tabular data
  5. Author-email: Sergey Astanin <s.astanin@gmail.com>
  6. License: MIT
  7. Project-URL: Homepage, https://github.com/astanin/python-tabulate
  8. Classifier: Development Status :: 4 - Beta
  9. Classifier: License :: OSI Approved :: MIT License
  10. Classifier: Operating System :: OS Independent
  11. Classifier: Programming Language :: Python :: 3
  12. Classifier: Programming Language :: Python :: 3.7
  13. Classifier: Programming Language :: Python :: 3.8
  14. Classifier: Programming Language :: Python :: 3.9
  15. Classifier: Programming Language :: Python :: 3.10
  16. Classifier: Topic :: Software Development :: Libraries
  17. Requires-Python: >=3.7
  18. Description-Content-Type: text/markdown
  19. License-File: LICENSE
  20. Provides-Extra: widechars
  21. Requires-Dist: wcwidth ; extra == 'widechars'
  22. python-tabulate
  23. ===============
  24. Pretty-print tabular data in Python, a library and a command-line
  25. utility.
  26. The main use cases of the library are:
  27. - printing small tables without hassle: just one function call,
  28. formatting is guided by the data itself
  29. - authoring tabular data for lightweight plain-text markup: multiple
  30. output formats suitable for further editing or transformation
  31. - readable presentation of mixed textual and numeric data: smart
  32. column alignment, configurable number formatting, alignment by a
  33. decimal point
  34. Installation
  35. ------------
  36. To install the Python library and the command line utility, run:
  37. ```shell
  38. pip install tabulate
  39. ```
  40. The command line utility will be installed as `tabulate` to `bin` on
  41. Linux (e.g. `/usr/bin`); or as `tabulate.exe` to `Scripts` in your
  42. Python installation on Windows (e.g. `C:\Python39\Scripts\tabulate.exe`).
  43. You may consider installing the library only for the current user:
  44. ```shell
  45. pip install tabulate --user
  46. ```
  47. In this case the command line utility will be installed to
  48. `~/.local/bin/tabulate` on Linux and to
  49. `%APPDATA%\Python\Scripts\tabulate.exe` on Windows.
  50. To install just the library on Unix-like operating systems:
  51. ```shell
  52. TABULATE_INSTALL=lib-only pip install tabulate
  53. ```
  54. On Windows:
  55. ```shell
  56. set TABULATE_INSTALL=lib-only
  57. pip install tabulate
  58. ```
  59. Build status
  60. ------------
  61. [![Build status](https://circleci.com/gh/astanin/python-tabulate.svg?style=svg)](https://circleci.com/gh/astanin/python-tabulate/tree/master) [![Build status](https://ci.appveyor.com/api/projects/status/8745yksvvol7h3d7/branch/master?svg=true)](https://ci.appveyor.com/project/astanin/python-tabulate/branch/master)
  62. Library usage
  63. -------------
  64. The module provides just one function, `tabulate`, which takes a list of
  65. lists or another tabular data type as the first argument, and outputs a
  66. nicely formatted plain-text table:
  67. ```pycon
  68. >>> from tabulate import tabulate
  69. >>> table = [["Sun",696000,1989100000],["Earth",6371,5973.6],
  70. ... ["Moon",1737,73.5],["Mars",3390,641.85]]
  71. >>> print(tabulate(table))
  72. ----- ------ -------------
  73. Sun 696000 1.9891e+09
  74. Earth 6371 5973.6
  75. Moon 1737 73.5
  76. Mars 3390 641.85
  77. ----- ------ -------------
  78. ```
  79. The following tabular data types are supported:
  80. - list of lists or another iterable of iterables
  81. - list or another iterable of dicts (keys as columns)
  82. - dict of iterables (keys as columns)
  83. - list of dataclasses (Python 3.7+ only, field names as columns)
  84. - two-dimensional NumPy array
  85. - NumPy record arrays (names as columns)
  86. - pandas.DataFrame
  87. Tabulate is a Python3 library.
  88. ### Headers
  89. The second optional argument named `headers` defines a list of column
  90. headers to be used:
  91. ```pycon
  92. >>> print(tabulate(table, headers=["Planet","R (km)", "mass (x 10^29 kg)"]))
  93. Planet R (km) mass (x 10^29 kg)
  94. -------- -------- -------------------
  95. Sun 696000 1.9891e+09
  96. Earth 6371 5973.6
  97. Moon 1737 73.5
  98. Mars 3390 641.85
  99. ```
  100. If `headers="firstrow"`, then the first row of data is used:
  101. ```pycon
  102. >>> print(tabulate([["Name","Age"],["Alice",24],["Bob",19]],
  103. ... headers="firstrow"))
  104. Name Age
  105. ------ -----
  106. Alice 24
  107. Bob 19
  108. ```
  109. If `headers="keys"`, then the keys of a dictionary/dataframe, or column
  110. indices are used. It also works for NumPy record arrays and lists of
  111. dictionaries or named tuples:
  112. ```pycon
  113. >>> print(tabulate({"Name": ["Alice", "Bob"],
  114. ... "Age": [24, 19]}, headers="keys"))
  115. Age Name
  116. ----- ------
  117. 24 Alice
  118. 19 Bob
  119. ```
  120. ### Row Indices
  121. By default, only pandas.DataFrame tables have an additional column
  122. called row index. To add a similar column to any other type of table,
  123. pass `showindex="always"` or `showindex=True` argument to `tabulate()`.
  124. To suppress row indices for all types of data, pass `showindex="never"`
  125. or `showindex=False`. To add a custom row index column, pass
  126. `showindex=rowIDs`, where `rowIDs` is some iterable:
  127. ```pycon
  128. >>> print(tabulate([["F",24],["M",19]], showindex="always"))
  129. - - --
  130. 0 F 24
  131. 1 M 19
  132. - - --
  133. ```
  134. ### Table format
  135. There is more than one way to format a table in plain text. The third
  136. optional argument named `tablefmt` defines how the table is formatted.
  137. Supported table formats are:
  138. - "plain"
  139. - "simple"
  140. - "github"
  141. - "grid"
  142. - "simple\_grid"
  143. - "rounded\_grid"
  144. - "heavy\_grid"
  145. - "mixed\_grid"
  146. - "double\_grid"
  147. - "fancy\_grid"
  148. - "outline"
  149. - "simple\_outline"
  150. - "rounded\_outline"
  151. - "heavy\_outline"
  152. - "mixed\_outline"
  153. - "double\_outline"
  154. - "fancy\_outline"
  155. - "pipe"
  156. - "orgtbl"
  157. - "asciidoc"
  158. - "jira"
  159. - "presto"
  160. - "pretty"
  161. - "psql"
  162. - "rst"
  163. - "mediawiki"
  164. - "moinmoin"
  165. - "youtrack"
  166. - "html"
  167. - "unsafehtml"
  168. - "latex"
  169. - "latex\_raw"
  170. - "latex\_booktabs"
  171. - "latex\_longtable"
  172. - "textile"
  173. - "tsv"
  174. `plain` tables do not use any pseudo-graphics to draw lines:
  175. ```pycon
  176. >>> table = [["spam",42],["eggs",451],["bacon",0]]
  177. >>> headers = ["item", "qty"]
  178. >>> print(tabulate(table, headers, tablefmt="plain"))
  179. item qty
  180. spam 42
  181. eggs 451
  182. bacon 0
  183. ```
  184. `simple` is the default format (the default may change in future
  185. versions). It corresponds to `simple_tables` in [Pandoc Markdown
  186. extensions](http://johnmacfarlane.net/pandoc/README.html#tables):
  187. ```pycon
  188. >>> print(tabulate(table, headers, tablefmt="simple"))
  189. item qty
  190. ------ -----
  191. spam 42
  192. eggs 451
  193. bacon 0
  194. ```
  195. `github` follows the conventions of GitHub flavored Markdown. It
  196. corresponds to the `pipe` format without alignment colons:
  197. ```pycon
  198. >>> print(tabulate(table, headers, tablefmt="github"))
  199. | item | qty |
  200. |--------|-------|
  201. | spam | 42 |
  202. | eggs | 451 |
  203. | bacon | 0 |
  204. ```
  205. `grid` is like tables formatted by Emacs'
  206. [table.el](http://table.sourceforge.net/) package. It corresponds to
  207. `grid_tables` in Pandoc Markdown extensions:
  208. ```pycon
  209. >>> print(tabulate(table, headers, tablefmt="grid"))
  210. +--------+-------+
  211. | item | qty |
  212. +========+=======+
  213. | spam | 42 |
  214. +--------+-------+
  215. | eggs | 451 |
  216. +--------+-------+
  217. | bacon | 0 |
  218. +--------+-------+
  219. ```
  220. `simple_grid` draws a grid using single-line box-drawing characters:
  221. >>> print(tabulate(table, headers, tablefmt="simple_grid"))
  222. ┌────────┬───────┐
  223. │ item │ qty │
  224. ├────────┼───────┤
  225. │ spam │ 42 │
  226. ├────────┼───────┤
  227. │ eggs │ 451 │
  228. ├────────┼───────┤
  229. │ bacon │ 0 │
  230. └────────┴───────┘
  231. `rounded_grid` draws a grid using single-line box-drawing characters with rounded corners:
  232. >>> print(tabulate(table, headers, tablefmt="rounded_grid"))
  233. ╭────────┬───────╮
  234. │ item │ qty │
  235. ├────────┼───────┤
  236. │ spam │ 42 │
  237. ├────────┼───────┤
  238. │ eggs │ 451 │
  239. ├────────┼───────┤
  240. │ bacon │ 0 │
  241. ╰────────┴───────╯
  242. `heavy_grid` draws a grid using bold (thick) single-line box-drawing characters:
  243. >>> print(tabulate(table, headers, tablefmt="heavy_grid"))
  244. ┏━━━━━━━━┳━━━━━━━┓
  245. ┃ item ┃ qty ┃
  246. ┣━━━━━━━━╋━━━━━━━┫
  247. ┃ spam ┃ 42 ┃
  248. ┣━━━━━━━━╋━━━━━━━┫
  249. ┃ eggs ┃ 451 ┃
  250. ┣━━━━━━━━╋━━━━━━━┫
  251. ┃ bacon ┃ 0 ┃
  252. ┗━━━━━━━━┻━━━━━━━┛
  253. `mixed_grid` draws a grid using a mix of light (thin) and heavy (thick) lines box-drawing characters:
  254. >>> print(tabulate(table, headers, tablefmt="mixed_grid"))
  255. ┍━━━━━━━━┯━━━━━━━┑
  256. │ item │ qty │
  257. ┝━━━━━━━━┿━━━━━━━┥
  258. │ spam │ 42 │
  259. ├────────┼───────┤
  260. │ eggs │ 451 │
  261. ├────────┼───────┤
  262. │ bacon │ 0 │
  263. ┕━━━━━━━━┷━━━━━━━┙
  264. `double_grid` draws a grid using double-line box-drawing characters:
  265. >>> print(tabulate(table, headers, tablefmt="double_grid"))
  266. ╔════════╦═══════╗
  267. ║ item ║ qty ║
  268. ╠════════╬═══════╣
  269. ║ spam ║ 42 ║
  270. ╠════════╬═══════╣
  271. ║ eggs ║ 451 ║
  272. ╠════════╬═══════╣
  273. ║ bacon ║ 0 ║
  274. ╚════════╩═══════╝
  275. `fancy_grid` draws a grid using a mix of single and
  276. double-line box-drawing characters:
  277. ```pycon
  278. >>> print(tabulate(table, headers, tablefmt="fancy_grid"))
  279. ╒════════╤═══════╕
  280. │ item │ qty │
  281. ╞════════╪═══════╡
  282. │ spam │ 42 │
  283. ├────────┼───────┤
  284. │ eggs │ 451 │
  285. ├────────┼───────┤
  286. │ bacon │ 0 │
  287. ╘════════╧═══════╛
  288. ```
  289. `outline` is the same as the `grid` format but doesn't draw lines between rows:
  290. >>> print(tabulate(table, headers, tablefmt="outline"))
  291. +--------+-------+
  292. | item | qty |
  293. +========+=======+
  294. | spam | 42 |
  295. | eggs | 451 |
  296. | bacon | 0 |
  297. +--------+-------+
  298. `simple_outline` is the same as the `simple_grid` format but doesn't draw lines between rows:
  299. >>> print(tabulate(table, headers, tablefmt="simple_outline"))
  300. ┌────────┬───────┐
  301. │ item │ qty │
  302. ├────────┼───────┤
  303. │ spam │ 42 │
  304. │ eggs │ 451 │
  305. │ bacon │ 0 │
  306. └────────┴───────┘
  307. `rounded_outline` is the same as the `rounded_grid` format but doesn't draw lines between rows:
  308. >>> print(tabulate(table, headers, tablefmt="rounded_outline"))
  309. ╭────────┬───────╮
  310. │ item │ qty │
  311. ├────────┼───────┤
  312. │ spam │ 42 │
  313. │ eggs │ 451 │
  314. │ bacon │ 0 │
  315. ╰────────┴───────╯
  316. `heavy_outline` is the same as the `heavy_grid` format but doesn't draw lines between rows:
  317. >>> print(tabulate(table, headers, tablefmt="heavy_outline"))
  318. ┏━━━━━━━━┳━━━━━━━┓
  319. ┃ item ┃ qty ┃
  320. ┣━━━━━━━━╋━━━━━━━┫
  321. ┃ spam ┃ 42 ┃
  322. ┃ eggs ┃ 451 ┃
  323. ┃ bacon ┃ 0 ┃
  324. ┗━━━━━━━━┻━━━━━━━┛
  325. `mixed_outline` is the same as the `mixed_grid` format but doesn't draw lines between rows:
  326. >>> print(tabulate(table, headers, tablefmt="mixed_outline"))
  327. ┍━━━━━━━━┯━━━━━━━┑
  328. │ item │ qty │
  329. ┝━━━━━━━━┿━━━━━━━┥
  330. │ spam │ 42 │
  331. │ eggs │ 451 │
  332. │ bacon │ 0 │
  333. ┕━━━━━━━━┷━━━━━━━┙
  334. `double_outline` is the same as the `double_grid` format but doesn't draw lines between rows:
  335. >>> print(tabulate(table, headers, tablefmt="double_outline"))
  336. ╔════════╦═══════╗
  337. ║ item ║ qty ║
  338. ╠════════╬═══════╣
  339. ║ spam ║ 42 ║
  340. ║ eggs ║ 451 ║
  341. ║ bacon ║ 0 ║
  342. ╚════════╩═══════╝
  343. `fancy_outline` is the same as the `fancy_grid` format but doesn't draw lines between rows:
  344. >>> print(tabulate(table, headers, tablefmt="fancy_outline"))
  345. ╒════════╤═══════╕
  346. │ item │ qty │
  347. ╞════════╪═══════╡
  348. │ spam │ 42 │
  349. │ eggs │ 451 │
  350. │ bacon │ 0 │
  351. ╘════════╧═══════╛
  352. `presto` is like tables formatted by Presto cli:
  353. ```pycon
  354. >>> print(tabulate(table, headers, tablefmt="presto"))
  355. item | qty
  356. --------+-------
  357. spam | 42
  358. eggs | 451
  359. bacon | 0
  360. ```
  361. `pretty` attempts to be close to the format emitted by the PrettyTables
  362. library:
  363. ```pycon
  364. >>> print(tabulate(table, headers, tablefmt="pretty"))
  365. +-------+-----+
  366. | item | qty |
  367. +-------+-----+
  368. | spam | 42 |
  369. | eggs | 451 |
  370. | bacon | 0 |
  371. +-------+-----+
  372. ```
  373. `psql` is like tables formatted by Postgres' psql cli:
  374. ```pycon
  375. >>> print(tabulate(table, headers, tablefmt="psql"))
  376. +--------+-------+
  377. | item | qty |
  378. |--------+-------|
  379. | spam | 42 |
  380. | eggs | 451 |
  381. | bacon | 0 |
  382. +--------+-------+
  383. ```
  384. `pipe` follows the conventions of [PHP Markdown
  385. Extra](http://michelf.ca/projects/php-markdown/extra/#table) extension.
  386. It corresponds to `pipe_tables` in Pandoc. This format uses colons to
  387. indicate column alignment:
  388. ```pycon
  389. >>> print(tabulate(table, headers, tablefmt="pipe"))
  390. | item | qty |
  391. |:-------|------:|
  392. | spam | 42 |
  393. | eggs | 451 |
  394. | bacon | 0 |
  395. ```
  396. `asciidoc` formats data like a simple table of the
  397. [AsciiDoctor](https://docs.asciidoctor.org/asciidoc/latest/syntax-quick-reference/#tables)
  398. format:
  399. ```pycon
  400. >>> print(tabulate(table, headers, tablefmt="asciidoc"))
  401. [cols="8<,7>",options="header"]
  402. |====
  403. | item | qty
  404. | spam | 42
  405. | eggs | 451
  406. | bacon | 0
  407. |====
  408. ```
  409. `orgtbl` follows the conventions of Emacs
  410. [org-mode](http://orgmode.org/manual/Tables.html), and is editable also
  411. in the minor orgtbl-mode. Hence its name:
  412. ```pycon
  413. >>> print(tabulate(table, headers, tablefmt="orgtbl"))
  414. | item | qty |
  415. |--------+-------|
  416. | spam | 42 |
  417. | eggs | 451 |
  418. | bacon | 0 |
  419. ```
  420. `jira` follows the conventions of Atlassian Jira markup language:
  421. ```pycon
  422. >>> print(tabulate(table, headers, tablefmt="jira"))
  423. || item || qty ||
  424. | spam | 42 |
  425. | eggs | 451 |
  426. | bacon | 0 |
  427. ```
  428. `rst` formats data like a simple table of the
  429. [reStructuredText](http://docutils.sourceforge.net/docs/user/rst/quickref.html#tables)
  430. format:
  431. ```pycon
  432. >>> print(tabulate(table, headers, tablefmt="rst"))
  433. ====== =====
  434. item qty
  435. ====== =====
  436. spam 42
  437. eggs 451
  438. bacon 0
  439. ====== =====
  440. ```
  441. `mediawiki` format produces a table markup used in
  442. [Wikipedia](http://www.mediawiki.org/wiki/Help:Tables) and on other
  443. MediaWiki-based sites:
  444. ```pycon
  445. >>> print(tabulate(table, headers, tablefmt="mediawiki"))
  446. {| class="wikitable" style="text-align: left;"
  447. |+ <!-- caption -->
  448. |-
  449. ! item !! align="right"| qty
  450. |-
  451. | spam || align="right"| 42
  452. |-
  453. | eggs || align="right"| 451
  454. |-
  455. | bacon || align="right"| 0
  456. |}
  457. ```
  458. `moinmoin` format produces a table markup used in
  459. [MoinMoin](https://moinmo.in/) wikis:
  460. ```pycon
  461. >>> print(tabulate(table, headers, tablefmt="moinmoin"))
  462. || ''' item ''' || ''' quantity ''' ||
  463. || spam || 41.999 ||
  464. || eggs || 451 ||
  465. || bacon || ||
  466. ```
  467. `youtrack` format produces a table markup used in Youtrack tickets:
  468. ```pycon
  469. >>> print(tabulate(table, headers, tablefmt="youtrack"))
  470. || item || quantity ||
  471. | spam | 41.999 |
  472. | eggs | 451 |
  473. | bacon | |
  474. ```
  475. `textile` format produces a table markup used in
  476. [Textile](http://redcloth.org/hobix.com/textile/) format:
  477. ```pycon
  478. >>> print(tabulate(table, headers, tablefmt="textile"))
  479. |_. item |_. qty |
  480. |<. spam |>. 42 |
  481. |<. eggs |>. 451 |
  482. |<. bacon |>. 0 |
  483. ```
  484. `html` produces standard HTML markup as an html.escape'd str
  485. with a ._repr_html_ method so that Jupyter Lab and Notebook display the HTML
  486. and a .str property so that the raw HTML remains accessible.
  487. `unsafehtml` table format can be used if an unescaped HTML is required:
  488. ```pycon
  489. >>> print(tabulate(table, headers, tablefmt="html"))
  490. <table>
  491. <tbody>
  492. <tr><th>item </th><th style="text-align: right;"> qty</th></tr>
  493. <tr><td>spam </td><td style="text-align: right;"> 42</td></tr>
  494. <tr><td>eggs </td><td style="text-align: right;"> 451</td></tr>
  495. <tr><td>bacon </td><td style="text-align: right;"> 0</td></tr>
  496. </tbody>
  497. </table>
  498. ```
  499. `latex` format creates a `tabular` environment for LaTeX markup,
  500. replacing special characters like `_` or `\` to their LaTeX
  501. correspondents:
  502. ```pycon
  503. >>> print(tabulate(table, headers, tablefmt="latex"))
  504. \begin{tabular}{lr}
  505. \hline
  506. item & qty \\
  507. \hline
  508. spam & 42 \\
  509. eggs & 451 \\
  510. bacon & 0 \\
  511. \hline
  512. \end{tabular}
  513. ```
  514. `latex_raw` behaves like `latex` but does not escape LaTeX commands and
  515. special characters.
  516. `latex_booktabs` creates a `tabular` environment for LaTeX markup using
  517. spacing and style from the `booktabs` package.
  518. `latex_longtable` creates a table that can stretch along multiple pages,
  519. using the `longtable` package.
  520. ### Column alignment
  521. `tabulate` is smart about column alignment. It detects columns which
  522. contain only numbers, and aligns them by a decimal point (or flushes
  523. them to the right if they appear to be integers). Text columns are
  524. flushed to the left.
  525. You can override the default alignment with `numalign` and `stralign`
  526. named arguments. Possible column alignments are: `right`, `center`,
  527. `left`, `decimal` (only for numbers), and `None` (to disable alignment).
  528. Aligning by a decimal point works best when you need to compare numbers
  529. at a glance:
  530. ```pycon
  531. >>> print(tabulate([[1.2345],[123.45],[12.345],[12345],[1234.5]]))
  532. ----------
  533. 1.2345
  534. 123.45
  535. 12.345
  536. 12345
  537. 1234.5
  538. ----------
  539. ```
  540. Compare this with a more common right alignment:
  541. ```pycon
  542. >>> print(tabulate([[1.2345],[123.45],[12.345],[12345],[1234.5]], numalign="right"))
  543. ------
  544. 1.2345
  545. 123.45
  546. 12.345
  547. 12345
  548. 1234.5
  549. ------
  550. ```
  551. For `tabulate`, anything which can be parsed as a number is a number.
  552. Even numbers represented as strings are aligned properly. This feature
  553. comes in handy when reading a mixed table of text and numbers from a
  554. file:
  555. ```pycon
  556. >>> import csv ; from StringIO import StringIO
  557. >>> table = list(csv.reader(StringIO("spam, 42\neggs, 451\n")))
  558. >>> table
  559. [['spam', ' 42'], ['eggs', ' 451']]
  560. >>> print(tabulate(table))
  561. ---- ----
  562. spam 42
  563. eggs 451
  564. ---- ----
  565. ```
  566. To disable this feature use `disable_numparse=True`.
  567. ```pycon
  568. >>> print(tabulate.tabulate([["Ver1", "18.0"], ["Ver2","19.2"]], tablefmt="simple", disable_numparse=True))
  569. ---- ----
  570. Ver1 18.0
  571. Ver2 19.2
  572. ---- ----
  573. ```
  574. ### Custom column alignment
  575. `tabulate` allows a custom column alignment to override the above. The
  576. `colalign` argument can be a list or a tuple of `stralign` named
  577. arguments. Possible column alignments are: `right`, `center`, `left`,
  578. `decimal` (only for numbers), and `None` (to disable alignment).
  579. Omitting an alignment uses the default. For example:
  580. ```pycon
  581. >>> print(tabulate([["one", "two"], ["three", "four"]], colalign=("right",))
  582. ----- ----
  583. one two
  584. three four
  585. ----- ----
  586. ```
  587. ### Number formatting
  588. `tabulate` allows to define custom number formatting applied to all
  589. columns of decimal numbers. Use `floatfmt` named argument:
  590. ```pycon
  591. >>> print(tabulate([["pi",3.141593],["e",2.718282]], floatfmt=".4f"))
  592. -- ------
  593. pi 3.1416
  594. e 2.7183
  595. -- ------
  596. ```
  597. `floatfmt` argument can be a list or a tuple of format strings, one per
  598. column, in which case every column may have different number formatting:
  599. ```pycon
  600. >>> print(tabulate([[0.12345, 0.12345, 0.12345]], floatfmt=(".1f", ".3f")))
  601. --- ----- -------
  602. 0.1 0.123 0.12345
  603. --- ----- -------
  604. ```
  605. `intfmt` works similarly for integers
  606. >>> print(tabulate([["a",1000],["b",90000]], intfmt=","))
  607. - ------
  608. a 1,000
  609. b 90,000
  610. - ------
  611. ### Text formatting
  612. By default, `tabulate` removes leading and trailing whitespace from text
  613. columns. To disable whitespace removal, set the global module-level flag
  614. `PRESERVE_WHITESPACE`:
  615. ```python
  616. import tabulate
  617. tabulate.PRESERVE_WHITESPACE = True
  618. ```
  619. ### Wide (fullwidth CJK) symbols
  620. To properly align tables which contain wide characters (typically
  621. fullwidth glyphs from Chinese, Japanese or Korean languages), the user
  622. should install `wcwidth` library. To install it together with
  623. `tabulate`:
  624. ```shell
  625. pip install tabulate[widechars]
  626. ```
  627. Wide character support is enabled automatically if `wcwidth` library is
  628. already installed. To disable wide characters support without
  629. uninstalling `wcwidth`, set the global module-level flag
  630. `WIDE_CHARS_MODE`:
  631. ```python
  632. import tabulate
  633. tabulate.WIDE_CHARS_MODE = False
  634. ```
  635. ### Multiline cells
  636. Most table formats support multiline cell text (text containing newline
  637. characters). The newline characters are honored as line break
  638. characters.
  639. Multiline cells are supported for data rows and for header rows.
  640. Further automatic line breaks are not inserted. Of course, some output
  641. formats such as latex or html handle automatic formatting of the cell
  642. content on their own, but for those that don't, the newline characters
  643. in the input cell text are the only means to break a line in cell text.
  644. Note that some output formats (e.g. simple, or plain) do not represent
  645. row delimiters, so that the representation of multiline cells in such
  646. formats may be ambiguous to the reader.
  647. The following examples of formatted output use the following table with
  648. a multiline cell, and headers with a multiline cell:
  649. ```pycon
  650. >>> table = [["eggs",451],["more\nspam",42]]
  651. >>> headers = ["item\nname", "qty"]
  652. ```
  653. `plain` tables:
  654. ```pycon
  655. >>> print(tabulate(table, headers, tablefmt="plain"))
  656. item qty
  657. name
  658. eggs 451
  659. more 42
  660. spam
  661. ```
  662. `simple` tables:
  663. ```pycon
  664. >>> print(tabulate(table, headers, tablefmt="simple"))
  665. item qty
  666. name
  667. ------ -----
  668. eggs 451
  669. more 42
  670. spam
  671. ```
  672. `grid` tables:
  673. ```pycon
  674. >>> print(tabulate(table, headers, tablefmt="grid"))
  675. +--------+-------+
  676. | item | qty |
  677. | name | |
  678. +========+=======+
  679. | eggs | 451 |
  680. +--------+-------+
  681. | more | 42 |
  682. | spam | |
  683. +--------+-------+
  684. ```
  685. `fancy_grid` tables:
  686. ```pycon
  687. >>> print(tabulate(table, headers, tablefmt="fancy_grid"))
  688. ╒════════╤═══════╕
  689. │ item │ qty │
  690. │ name │ │
  691. ╞════════╪═══════╡
  692. │ eggs │ 451 │
  693. ├────────┼───────┤
  694. │ more │ 42 │
  695. │ spam │ │
  696. ╘════════╧═══════╛
  697. ```
  698. `pipe` tables:
  699. ```pycon
  700. >>> print(tabulate(table, headers, tablefmt="pipe"))
  701. | item | qty |
  702. | name | |
  703. |:-------|------:|
  704. | eggs | 451 |
  705. | more | 42 |
  706. | spam | |
  707. ```
  708. `orgtbl` tables:
  709. ```pycon
  710. >>> print(tabulate(table, headers, tablefmt="orgtbl"))
  711. | item | qty |
  712. | name | |
  713. |--------+-------|
  714. | eggs | 451 |
  715. | more | 42 |
  716. | spam | |
  717. ```
  718. `jira` tables:
  719. ```pycon
  720. >>> print(tabulate(table, headers, tablefmt="jira"))
  721. | item | qty |
  722. | name | |
  723. |:-------|------:|
  724. | eggs | 451 |
  725. | more | 42 |
  726. | spam | |
  727. ```
  728. `presto` tables:
  729. ```pycon
  730. >>> print(tabulate(table, headers, tablefmt="presto"))
  731. item | qty
  732. name |
  733. --------+-------
  734. eggs | 451
  735. more | 42
  736. spam |
  737. ```
  738. `pretty` tables:
  739. ```pycon
  740. >>> print(tabulate(table, headers, tablefmt="pretty"))
  741. +------+-----+
  742. | item | qty |
  743. | name | |
  744. +------+-----+
  745. | eggs | 451 |
  746. | more | 42 |
  747. | spam | |
  748. +------+-----+
  749. ```
  750. `psql` tables:
  751. ```pycon
  752. >>> print(tabulate(table, headers, tablefmt="psql"))
  753. +--------+-------+
  754. | item | qty |
  755. | name | |
  756. |--------+-------|
  757. | eggs | 451 |
  758. | more | 42 |
  759. | spam | |
  760. +--------+-------+
  761. ```
  762. `rst` tables:
  763. ```pycon
  764. >>> print(tabulate(table, headers, tablefmt="rst"))
  765. ====== =====
  766. item qty
  767. name
  768. ====== =====
  769. eggs 451
  770. more 42
  771. spam
  772. ====== =====
  773. ```
  774. Multiline cells are not well-supported for the other table formats.
  775. ### Automating Multilines
  776. While tabulate supports data passed in with multilines entries explicitly provided,
  777. it also provides some support to help manage this work internally.
  778. The `maxcolwidths` argument is a list where each entry specifies the max width for
  779. it's respective column. Any cell that will exceed this will automatically wrap the content.
  780. To assign the same max width for all columns, a singular int scaler can be used.
  781. Use `None` for any columns where an explicit maximum does not need to be provided,
  782. and thus no automate multiline wrapping will take place.
  783. The wrapping uses the python standard [textwrap.wrap](https://docs.python.org/3/library/textwrap.html#textwrap.wrap)
  784. function with default parameters - aside from width.
  785. This example demonstrates usage of automatic multiline wrapping, though typically
  786. the lines being wrapped would probably be significantly longer than this.
  787. ```pycon
  788. >>> print(tabulate([["John Smith", "Middle Manager"]], headers=["Name", "Title"], tablefmt="grid", maxcolwidths=[None, 8]))
  789. +------------+---------+
  790. | Name | Title |
  791. +============+=========+
  792. | John Smith | Middle |
  793. | | Manager |
  794. +------------+---------+
  795. ```
  796. ### Adding Separating lines
  797. One might want to add one or more separating lines to highlight different sections in a table.
  798. The separating lines will be of the same type as the one defined by the specified formatter as either the
  799. linebetweenrows, linebelowheader, linebelow, lineabove or just a simple empty line when none is defined for the formatter
  800. >>> from tabulate import tabulate, SEPARATING_LINE
  801. table = [["Earth",6371],
  802. ["Mars",3390],
  803. SEPARATING_LINE,
  804. ["Moon",1737]]
  805. print(tabulate(table, tablefmt="simple"))
  806. ----- ----
  807. Earth 6371
  808. Mars 3390
  809. ----- ----
  810. Moon 1737
  811. ----- ----
  812. ### ANSI support
  813. ANSI escape codes are non-printable byte sequences usually used for terminal operations like setting
  814. color output or modifying cursor positions. Because multi-byte ANSI sequences are inherently non-printable,
  815. they can still introduce unwanted extra length to strings. For example:
  816. >>> len('\033[31mthis text is red\033[0m') # printable length is 16
  817. 25
  818. To deal with this, string lengths are calculated after first removing all ANSI escape sequences. This ensures
  819. that the actual printable length is used for column widths, rather than the byte length. In the final, printable
  820. table, however, ANSI escape sequences are not removed so the original styling is preserved.
  821. Some terminals support a special grouping of ANSI escape sequences that are intended to display hyperlinks
  822. much in the same way they are shown in browsers. These are handled just as mentioned before: non-printable
  823. ANSI escape sequences are removed prior to string length calculation. The only diifference with escaped
  824. hyperlinks is that column width will be based on the length of the URL _text_ rather than the URL
  825. itself (terminals would show this text). For example:
  826. >>> len('\x1b]8;;https://example.com\x1b\\example\x1b]8;;\x1b\\') # display length is 7, showing 'example'
  827. 45
  828. Usage of the command line utility
  829. ---------------------------------
  830. Usage: tabulate [options] [FILE ...]
  831. FILE a filename of the file with tabular data;
  832. if "-" or missing, read data from stdin.
  833. Options:
  834. -h, --help show this message
  835. -1, --header use the first row of data as a table header
  836. -o FILE, --output FILE print table to FILE (default: stdout)
  837. -s REGEXP, --sep REGEXP use a custom column separator (default: whitespace)
  838. -F FPFMT, --float FPFMT floating point number format (default: g)
  839. -I INTFMT, --int INTFMT integer point number format (default: "")
  840. -f FMT, --format FMT set output table format; supported formats:
  841. plain, simple, github, grid, fancy_grid, pipe,
  842. orgtbl, rst, mediawiki, html, latex, latex_raw,
  843. latex_booktabs, latex_longtable, tsv
  844. (default: simple)
  845. Performance considerations
  846. --------------------------
  847. Such features as decimal point alignment and trying to parse everything
  848. as a number imply that `tabulate`:
  849. - has to "guess" how to print a particular tabular data type
  850. - needs to keep the entire table in-memory
  851. - has to "transpose" the table twice
  852. - does much more work than it may appear
  853. It may not be suitable for serializing really big tables (but who's
  854. going to do that, anyway?) or printing tables in performance sensitive
  855. applications. `tabulate` is about two orders of magnitude slower than
  856. simply joining lists of values with a tab, comma, or other separator.
  857. At the same time, `tabulate` is comparable to other table
  858. pretty-printers. Given a 10x10 table (a list of lists) of mixed text and
  859. numeric data, `tabulate` appears to be slower than `asciitable`, and
  860. faster than `PrettyTable` and `texttable` The following mini-benchmark
  861. was run in Python 3.9.13 on Windows 10:
  862. ================================= ========== ===========
  863. Table formatter time, μs rel. time
  864. ================================= ========== ===========
  865. csv to StringIO 12.5 1.0
  866. join with tabs and newlines 14.6 1.2
  867. asciitable (0.8.0) 192.0 15.4
  868. tabulate (0.9.0) 483.5 38.7
  869. tabulate (0.9.0, WIDE_CHARS_MODE) 637.6 51.1
  870. PrettyTable (3.4.1) 1080.6 86.6
  871. texttable (1.6.4) 1390.3 111.4
  872. ================================= ========== ===========
  873. Version history
  874. ---------------
  875. The full version history can be found at the [changelog](https://github.com/astanin/python-tabulate/blob/master/CHANGELOG).
  876. How to contribute
  877. -----------------
  878. Contributions should include tests and an explanation for the changes
  879. they propose. Documentation (examples, docstrings, README.md) should be
  880. updated accordingly.
  881. This project uses [pytest](https://docs.pytest.org/) testing
  882. framework and [tox](https://tox.readthedocs.io/) to automate testing in
  883. different environments. Add tests to one of the files in the `test/`
  884. folder.
  885. To run tests on all supported Python versions, make sure all Python
  886. interpreters, `pytest` and `tox` are installed, then run `tox` in the root
  887. of the project source tree.
  888. On Linux `tox` expects to find executables like `python3.7`, `python3.8` etc.
  889. On Windows it looks for `C:\Python37\python.exe`, `C:\Python38\python.exe` etc. respectively.
  890. One way to install all the required versions of the Python interpreter is to use [pyenv](https://github.com/pyenv/pyenv).
  891. All versions can then be easily installed with something like:
  892. pyenv install 3.7.12
  893. pyenv install 3.8.12
  894. ...
  895. Don't forget to change your `PATH` so that `tox` knows how to find all the installed versions. Something like
  896. export PATH="${PATH}:${HOME}/.pyenv/shims"
  897. To test only some Python environments, use `-e` option. For example, to
  898. test only against Python 3.7 and Python 3.10, run:
  899. ```shell
  900. tox -e py37,py310
  901. ```
  902. in the root of the project source tree.
  903. To enable NumPy and Pandas tests, run:
  904. ```shell
  905. tox -e py37-extra,py310-extra
  906. ```
  907. (this may take a long time the first time, because NumPy and Pandas will
  908. have to be installed in the new virtual environments)
  909. To fix code formatting:
  910. ```shell
  911. tox -e lint
  912. ```
  913. See `tox.ini` file to learn how to use to test
  914. individual Python versions.
  915. Contributors
  916. ------------
  917. Sergey Astanin, Pau Tallada Crespí, Erwin Marsi, Mik Kocikowski, Bill
  918. Ryder, Zach Dwiel, Frederik Rietdijk, Philipp Bogensberger, Greg
  919. (anonymous), Stefan Tatschner, Emiel van Miltenburg, Brandon Bennett,
  920. Amjith Ramanujam, Jan Schulz, Simon Percivall, Javier Santacruz
  921. López-Cepero, Sam Denton, Alexey Ziyangirov, acaird, Cesar Sanchez,
  922. naught101, John Vandenberg, Zack Dever, Christian Clauss, Benjamin
  923. Maier, Andy MacKinlay, Thomas Roten, Jue Wang, Joe King, Samuel Phan,
  924. Nick Satterly, Daniel Robbins, Dmitry B, Lars Butler, Andreas Maier,
  925. Dick Marinus, Sébastien Celles, Yago González, Andrew Gaul, Wim Glenn,
  926. Jean Michel Rouly, Tim Gates, John Vandenberg, Sorin Sbarnea,
  927. Wes Turner, Andrew Tija, Marco Gorelli, Sean McGinnis, danja100,
  928. endolith, Dominic Davis-Foster, pavlocat, Daniel Aslau, paulc,
  929. Felix Yan, Shane Loretz, Frank Busse, Harsh Singh, Derek Weitzel,
  930. Vladimir Vrzić, 서승우 (chrd5273), Georgy Frolov, Christian Cwienk,
  931. Bart Broere, Vilhelm Prytz, Alexander Gažo, Hugo van Kemenade,
  932. jamescooke, Matt Warner, Jérôme Provensal, Kevin Deldycke,
  933. Kian-Meng Ang, Kevin Patterson, Shodhan Save, cleoold, KOLANICH,
  934. Vijaya Krishna Kasula, Furcy Pin, Christian Fibich, Shaun Duncan,
  935. Dimitri Papadopoulos.