3 GJ\%@sdZddlmZddlmZddlmZddlmZddlZddl Z edkr\dd l m Z n dd lm Z eddkrdd l m Z dd lmZedZeZeZeZeZeZeZd d ZnPddl mZ ddlm Z mZedZeZeZeZeZeZe!ZeZ"ddl#Z#dd Zy ddl$Z$Wne%k r.dZ$YnXdddgZ&dZ'dZ(dZ)dZ*dZ+e$dk Z,edddddgZ-eddddgZ.edd d!d"d#d$d%d&d'gZ/d(d)Z0d*d+Z1d,d-Z2d.d/Z3d0d1Z4d2d3Z5dd4d5Z6dd6d7Z7d8d9d:d;dd?d@dAdBdCdD Z8e8fdEdFZ9dGdHZ:e/e-ddIdJde-ddIdJdde-ddIdJde.ddJde.ddJddd d#gdKe/dddde.ddJde.ddJddddKe/e-dLdIdLdLe-dLdMdLdLe-dLdIdLdLe-dLdIdLdLe.dNdNdNe.dNdNdNdOddKe/e-dPdQdRdSe-dTdQdUdVe-dWdXdYdZe-d[dQd\d]e.d^d^d^e.d^d^d^dOddKe/e-dNdIdNdNe-dNdIdNdNdde.dNdNdNe.dNdNdNdOd gdKe/e1e1dde.dNdNdNe.dNdNdNdOd gdKe/de-dNdIdLdNdde.dNdNdNe.dNdNdNdOddKe/dddde.d_d_d_e.dNdNdNdOddKe/de-ddIdLddde.ddNde.ddNddOddKe/e-dLdIdLdLe-dNdIdLdNde-dLdIdLdLe.dNdNdNe.dNdNdNdOddKe/e-ddMdJde-ddMdJdde-ddMdJde.ddJde.ddJddddKe/e-d`dddae-dbddde-dbddde-dcdddee2ddee2dNdddKe/ddddee6d_dedfee6d_dOddKe/dddde.dgdhdhe.didjdkdOddKe/e4dde-dldddee5dmee5dndd gdKe/e7e-dodddde-dpddde9e9dOddKe/e7e-dodddde-dpdddee9idqee9idqdOddKe/ee7drdse-dtdddde-duddde9e9dOddKe/dddde.ddvde.ddvddddKe/dddde.dwdxdNe3dOddKdyZ;eZ?dzd{d|d}d~dddddd Z@ejAdZBejAdZCejAdZDejAdZEddZFddZGddZHefddZIddZJdddZKddZLddZMddZNddZOddZPddZQddZRddZSeTfddZUddZVddZWdddZXddZYdddZZdddZ[dddZ\ddZ]ddZ^dddZ_fd{e*dde+dddf ddZ`ddZaddZbddÄZcddńZdddDŽZeddɄZfdd˄Zgdd̈́ZhddτZiddфZjddӄZkeldkrejdS)zPretty-print tabular data.)print_function)unicode_literals) namedtuple)python_version_tupleN30)Iterable) izip_longest)partialcCs t|tS)N) isinstancefile)frp/Users/sshvans/Documents/Work/Quickstart/python-virtual-environments/env/lib/python3.6/site-packages/tabulate.py_is_filesr) zip_longest)reducer cCs t|tjS)N)r ioIOBase)r rrrr-stabulatetabulate_formatssimple_separated_formatz0.8.3FgLinebeginZhlinesependDataRow TableFormat lineabovelinebelowheaderlinebetweenrows linebelow headerrowdatarowpaddingwith_header_hidecCs\|}|d krd|ddS|dkr8dd|ddS|dkrPdd|dSd|Sd S) z~Return a segment of a horizontal line with optional colons which indicate column's alignment (as in `pipe` output format).rightdecimal-:centerrleftN)r)r*r)aligncolwidthwrrr_pipe_segment_with_colonsssr3cCs&ddt||D}ddj|dS)znReturn a horizontal line with optional colons to indicate column's alignment (as in `pipe` output format).cSsg|]\}}t||qSr)r3).0ar2rrr sz*_pipe_line_with_colons..|)zipjoin) colwidths colalignssegmentsrrr_pipe_line_with_colonssr=cs@dddddfddt||D}|d}||j|jS)Nrzalign="right"| zalign="center"| )r/r)r.r*cs(g|] \}}dj|d|dqS) r)get)r4cr5) alignmentrrr6sz-_mediawiki_row_with_attrs..r)r8r9rstrip) separator cell_valuesr:r;values_with_attrsZcolsepr)rAr_mediawiki_row_with_attrss rFcsH|dd7<dddddfddt||D}d d j|d S) Nrr>z<.z>.z=.)r/r)r.r*c3s"|]\}}j|d|VqdS)rN)r?)r4r5v)rArr sz*_textile_row_with_attrs..r7)r8r9)rDr:r;valuesr)rAr_textile_row_with_attrssrJcCsdjddgS)N zz)r9)Zcolwidths_ignoreZcolaligns_ignorerrr _html_begin_table_without_headersrLcs^dddddfddt||D}ddj|jd}d krZd jd d |d dg}|S)Nrz style="text-align: right;"z style="text-align: center;")r/r)r.r*cs&g|]\}}djj|d|qS)z<{0}{1}>{2}r)formatr?)r4r@r5)rAcelltagrrr6sz(_html_row_with_attrs..zzthrKz
zzz)r8r9rB)rNrDr:r;rEZrowhtmlr)rArNr_html_row_with_attrss rPcs8dddddfddt||D}dj|dS)Nrzz)r/r)r.r*cs.g|]&\}}djj|d|qS)z {0}{1} {2} r)rMr?)r4r@r5)rArNheaderrrr6sz(_moin_row_with_attrs..z||)r8r9)rNrDr:r;rQrEr)rArNrQr_moin_row_with_attrssrRcsDddddddjfdd|D}djd |d |r.rKz\begin{tabular}{}z\toprulez\hline)r9)r:r;booktabsZtabular_columns_fmtr)rAr_latex_line_begin_tabularsrWz\&z\%z\$z\#z\_z\^{}z\{z\}z\textasciitilde{}z\textbackslash{}z\ensuremath{<}z\ensuremath{>}) &%$#_^{rU~\<>cs4fddfdd|D}tddd}t||S)Ncs j||S)N)r?)r@)escrulesrr escape_charsz_latex_row..escape_charcsg|]}djt|qS)r)r9map)r4cell)rdrrr6sz_latex_row..rrXz\\)r_build_simple_row)rDr:r;rcZescaped_valuesrowfmtr)rdrcr _latex_rows  ricCsddd}t|}g}|r(||d|d<x2|D]*}t|}|rN||d|d<|j|q.W||fS)NcSs$t|ttfr|j rdS|SdS)Nz..)r _text_type _binary_typestrip)valrrr escape_emptysz._rst_escape_first_column..escape_emptyr)listappend)rowsheadersrnZ new_headersZnew_rowsrowZnew_rowrrr_rst_escape_first_columns rtr+z )r!r"r#r$r%r&r'r(+=r7r,u╒u═u╤u╕u╞u╪u╡u├u─u┼u┤u╘u╧u╛u│z||z.{| class="wikitable" style="text-align: left;"z |+ |-z|-z|}!z''')rQz|| z || z| z | z |z
rOtdz\hlinez\hline \end{tabular})rcT)rVz\midrulez\bottomrule \end{tabular} z|_. z|_.)simpleplaingrid fancy_gridZgithubpipeorgtbljiraprestopsqlrstZ mediawikiZmoinmoinZyoutrackhtmlZlatexZ latex_rawZlatex_booktabsZtsvZtextiler{rzr|r}r~rrrrr) r{rzr|r}r~rrrrrz \r|\n|\r\ns | | z%\x1b\[\d+[;\d]*m|\x1b\[\d*\;\d*\;\d*ms\[\d+[;\d]*m|\[\d*\;\d*\;\d*mc Cs(tddddtd|dtd|ddddS)zConstruct a simple TableFormat with columns separated by a separator. >>> tsv = simple_separated_format("\t") ; tabulate([["foo", 1], ["spam", 23]], tablefmt=tsv) == 'foo \t 1\nspam\t23' True Nrr)r%r&r'r()r r)rCrrrrs   c Cs*y ||}dSttfk r$dSXdS)NTF) ValueError TypeError)convstringnrrr_isconvertibles rcCsHtt|sdSt|ttfrDtjt|s8tjt|rD|jdkSdS)z >>> _isnumber("123.45") True >>> _isnumber("123") True >>> _isnumber("spam") False >>> _isnumber("123e45678") False >>> _isnumber("inf") True Finf-infnanT)rrr) rfloatr rjrkmathisinfisnanlower)rrrr _isnumbers  rcCs*t||kp(t|ts t|to(t||S)zG >>> _isint("123") True >>> _isint("123.45") False )typer rkrjr)rZinttyperrr_isints rcCs"t|tkp t|ttfo |dkS)zc >>> _isbool(True) True >>> _isbool("False") True >>> _isbool(1) False TrueFalse)rr)r _bool_typer rkrj)rrrr_isbools rcCs|r t|tst|tr t|}|dkr,tSt|dr:tSt|rFtSt|rV|rVt St|t rh|rht St |rx|rxt St|trtStSdS)a%The least generic type (type(None), int, float, str, unicode). >>> _type(None) is type(None) True >>> _type("foo") is type("") True >>> _type("1") is type(1) True >>> _type('42') is type(42) True >>> _type('42') is type(42) True N isoformat) r rjrk_strip_invisible _none_typehasattrrrrint _long_typerr)r has_invisiblenumparserrr_types$    rcCs^t|rVt|rdS|jd}|dkr4|jjdn|}|dkrPt||dSdSndSdS) zSymbols after a decimal point, -1 if the string lacks the decimal point. >>> _afterpoint("123.45") 2 >>> _afterpoint("1001") -1 >>> _afterpoint("eggs") -1 >>> _afterpoint("123e45") 2 r,.reNrr)rrrfindrlen)rposrrr _afterpoints  rcCsd|}|j|S)uLFlush right. >>> _padleft(6, 'яйца') == ' яйца' True z{0:>%ds})rM)widthsfmtrrr_padleft*srcCsd|}|j|S)uLFlush left. >>> _padright(6, 'яйца') == 'яйца ' True z{0:<%ds})rM)rrrrrr _padright5srcCsd|}|j|S)uNCenter string. >>> _padboth(6, 'яйца') == ' яйца ' True z{0:^%ds})rM)rrrrrr_padboth@srcCs|S)Nr)Z ignore_widthrrrr_padnoneKsrcCs*t|trtjtd|Stjtd|SdS)z"Remove invisible ANSI color codes.rN)r rjresub_invisible_codes_invisible_codes_bytes)rrrrrOs rcCsHtdk rtrtj}nt}t|ts,t|tr8|t|S|t|SdS)zVisible width of a printed string. ANSI color codes are removed. >>> _visible_width('hello'), _visible_width("world") (5, 5) N)wcwidthWIDE_CHARS_MODEwcswidthrr rjrkr)rZlen_fnrrr_visible_widthWs   rcCs.t|trttjt|Sttjt|SdS)N)r rjboolrsearch_multiline_codes_multiline_codes_bytes)rrrr _is_multilineis rcCstt|tjd|S)z1Visible width of a potentially multiline content.z[ ])maxrersplit)Z multiline_s line_width_fnrrr_multiline_widthpsrcs4|r tn|rtjnt|r,fdd}n}|S)z2Return a function to calculate visible cell width.cs t|S)N)r)r)rrr~sz"_choose_width_fn..)rrrr)renable_widechars is_multilinewidth_fnr)rr_choose_width_fnusrcs|dkr tsdd|D}t}n|dkr@ts:dd|D}t}np|dkr|r\dd|D}ndd|D}t|fd dt||D}t}n |st}ntsd d|D}t}||fS) Nr)cSsg|] }|jqSr)rl)r4rrrrr6sz._align_column_choose_padfn..r.cSsg|] }|jqSr)rl)r4rrrrr6sr*cSsg|]}tt|qSr)rr)r4rrrrr6scSsg|] }t|qSr)r)r4rrrrr6scs g|]\}}||dqS)r>r)r4rZdecs) maxdecimalsrrr6scSsg|] }|jqSr)rl)r4rrrrr6s)PRESERVE_WHITESPACErrrr8rr)stringsrArpadfnZdecimalsr)rr_align_column_choose_padfns, rc st|||\}t|||}tt||}tt|||r| r^| r^fdd|D}qdd|D} fddt|| D} fddt|| D}n`| r| rfdd|D}n>ttt|} fddt|| D} fddt|| D}|S) ztring] -> [padded_string]cs*g|]"}djfdd|jDqS)rKcsg|]}|qSrr)r4r)maxwidthrrrr6sz,_align_column...)r9 splitlines)r4ms)rrrrr6sz!_align_column..cSs&g|]}tddtjd|DqS)css|]}t|VqdS)N)r)r4rrrrrHsz+_align_column...z[ ])rrr)r4rrrrr6scsg|]\}}||qSrr)r4r2rS)rrrr6scs2g|]*\}djfdd|jp&|DqS)rKcsg|]}|qSrr)r4r)rr2rrr6sz,_align_column...)r9r)r4r)r)r2rr6scsg|]}|qSrr)r4r)rrrrr6scsg|]\}}||qSrr)r4r2rS)rrrr6scsg|]\}}||qSrr)r4rr2)rrrr6s)rrrorerr8r) rrAZminwidthrrrrZs_widthsZpadded_stringsZs_lensZvisible_widthsr)rrr _align_columns$      rc CsPtdtdtdtdtdtdi}ttttttd}t|j|d|j|d}||S)Nrr,r)rrrrr,r)rrrrrkrjrr?)Ztype1Ztype2typesZinvtypesZ moregenericrrr _more_genericsrcs fdd|D}tt|tS)u]The least generic type all column values are convertible to. >>> _column_type([True, False]) is _bool_type True >>> _column_type(["1", "2"]) is _int_type True >>> _column_type(["1", "2.3"]) is _float_type True >>> _column_type(["1", "2.3", "four"]) is _text_type True >>> _column_type(["four", 'пять']) is _text_type True >>> _column_type([None, "brux"]) is _text_type True >>> _column_type([1, 2, None]) is _int_type True >>> import datetime as dt >>> _column_type([dt.datetime(1991,2,19), dt.time(17,35)]) is _text_type True csg|]}t|qSr)r)r4r)rrrrr6sz _column_type..)rrr)rrrrr)rrr _column_typesrc Cs|dkr |S|ttgkr"dj|S|tkrPy t|dStk rLt|SXnZ|tkr|oht|ttf}|rt|}tt||}|j||Stt||Sn dj|SdS)uFormat a value accoding to its type. Unicode is supported: >>> hrow = ['буква', 'цифра'] ; tbl = [['аз', 2], ['буки', 4]] ; good_result = '\u0431\u0443\u043a\u0432\u0430 \u0446\u0438\u0444\u0440\u0430\n------- -------\n\u0430\u0437 2\n\u0431\u0443\u043a\u0438 4' ; tabulate(tbl, headers=hrow) == good_result True Nz{0}ascii) rrjrMrkrrr rreplace)rmZvaltypefloatfmt missingvalrZis_a_colored_numberZraw_valZ formatted_valrrr_formats"      rc s|r0tjt|}fdd|D}dj|St||}|7dkrVt|Sdkrht|Ssvdj|St|SdS)zIPad string header to width chars given known visible_width of the header.csg|]}t||qSr) _align_header)r4h)rArrrrr6 sz!_align_header..rKr/r.z{0}N) rrrr9rrrrMr) rQrArZ visible_widthrrZ header_linesZ padded_linesZ ninvisibler)rArrrrs      rcCsX|dks|dkr|St|t|kr@td|td|tdddt||D}|S)zAdd a left-most index column.NFzindex=zrows=z0index must be as long as the number of data rowscSsg|]\}}|gt|qSr)ro)r4rGrsrrrr6 sz&_prepend_row_index..)rprintrr8)rqindexrrr_prepend_row_indexs  rc Cs"yt|Stk rdSXdS)zDA wrapper around standard bool() which doesn't throw on NumPy arraysFN)rr)rmrrr_bool$srdefaultc sytd}Wn tk r0d}tYnXd}t|doHt|drt|jdrr|jtt|j}nxt|drt||jjdk rt |jjtr|jjdd<n|jjgdd<|j}t|j}d d |D}ntd dkrltt t nft|}dkr(| r(gnDdkrXt|d rXt |j d rX|j jndkrt|dkrt |dtrt|ddrtt t |djnt|dkrt |dtrt}gdkrt|dkr|dnijj|j|dd}x@|D]8}x0|jD]$} | |kr,j| |j| q,WqWdkrjn|t trfdd Dtt t nNdkrt|dkr҇fdd Dtt t ngnrtdfdd |D}nrdkr:t|dr:t|dr:t|dr:dd |jDn2dkrlt|dkrltt t tt|ddkrt|dkr|dk r|dgt|d|dd}n|dtt t |dd}tt t tt t|}t|t tgk} |dkr |dk r t||}nt |trD| rDt|t|}n`|dks`t|r| r|dkrzttt|}t||}n|dkst| r| rrt|dkrt} t|d} | | krdg| | |fS)aTransform a supported data type to a list of lists, and a list of headers. Supported tabular data types: * list-of-lists or another iterable of iterables * list of named tuples (usually used with headers="keys") * list of dicts (usually used with headers="keys") * list of OrderedDicts (usually used with headers="keys") * 2D NumPy arrays * NumPy record arrays (usually used with headers="keys") * dict of iterables (usually used with headers="keys") * pandas.DataFrame (usually used with headers="keys") The first row can be used as headers if headers="firstrow", column indices can be used as headers if headers="keys". If showindex="default", show row indices of the pandas.DataFrame. If showindex="always", show row indices for all types of data. If showindex="never", don't show row indices for all types of data. If showindex is an iterable, show its values as row indices. FTNkeysrI__call__rrcSsg|] }t|qSr)ro)r4rsrrrr6dsz+_normalize_tabular_data..z7tabular data doesn't appear to be a dict or a DataFramedtypenames_fieldsfirstrowr,csg|]}j||qSr)r?)r4k)rrrrr6scsg|]}j||qSr)r?)r4r) firstdictrrr6sz6headers for a list of dicts is not a dict or a keywordcsg|]fddDqS)csg|]}j|qSr)r?)r4r)rsrrr6sz6_normalize_tabular_data...r)r4)r)rsrr6s descriptionZfetchoneZrowcountcSsg|] }|dqS)rr)r4columnrrrr6sralwaysZneverr)rrrorrIrr rnamer rerjgetattrrrrtuplerdictsetextendupdaterpaddrrangerrkrrr) tabular_datarr showindexZis_headers2bool_brokenrrqvalsZ uniq_keysrsrZshowindex_is_a_strZnhsZncolsr)rrrrr_normalize_tabular_data,s                             rr*r/c s|dkr g}t|||d\} }|dkr4t| |\} }djdjtt|gdd| D} tjt| tdk opt |t krt | rt j ||}dndt tt| } t|t| } d dt| | D}t|trt| |g}n6t|}t|t| kr"|jt| t|tgt|tr>t| |g}n6t|}t|t| krt|jt| t|tgfd dt| |||D} fd d|D}| dk rt| tstxt| D]\}}|||<qW|rfd d|Dn d gt| }fddt| ||D} |r| p>> print(tabulate([[1, 2.34], [-56, "8.999"], ["2", "10001"]])) --- --------- 1 2.34 -56 8.999 2 10001 --- --------- The first required argument (`tabular_data`) can be a list-of-lists (or another iterable of iterables), a list of named tuples, a dictionary of iterables, an iterable of dictionaries, a two-dimensional NumPy array, NumPy record array, or a Pandas' dataframe. Table headers ------------- To print nice column headers, supply the second argument (`headers`): - `headers` can be an explicit list of column headers - if `headers="firstrow"`, then the first row of data is used - if `headers="keys"`, then dictionary keys or column indices are used Otherwise a headerless table is produced. If the number of headers is less than the number of columns, they are supposed to be names of the last columns. This is consistent with the plain-text format of R and Pandas' dataframes. >>> print(tabulate([["sex","age"],["Alice","F",24],["Bob","M",19]], ... headers="firstrow")) sex age ----- ----- ----- Alice F 24 Bob M 19 By default, pandas.DataFrame data have an additional column called row index. To add a similar column to all other types of data, use `showindex="always"` or `showindex=True`. To suppress row indices for all types of data, pass `showindex="never" or `showindex=False`. To add a custom row index column, pass `showindex=some_iterable`. >>> print(tabulate([["F",24],["M",19]], showindex="always")) - - -- 0 F 24 1 M 19 - - -- Column alignment ---------------- `tabulate` tries to detect column types automatically, and aligns the values properly. By default it aligns decimal points of the numbers (or flushes integer numbers to the right), and flushes everything else to the left. Possible column alignments (`numalign`, `stralign`) are: "right", "center", "left", "decimal" (only for `numalign`), and None (to disable alignment). Table formats ------------- `floatfmt` is a format specification used for columns which contain numeric data with a decimal point. This can also be a list or tuple of format strings, one per column. `None` values are replaced with a `missingval` string (like `floatfmt`, this can also be a list of values for different columns): >>> print(tabulate([["spam", 1, None], ... ["eggs", 42, 3.14], ... ["other", None, 2.7]], missingval="?")) ----- -- ---- spam 1 ? eggs 42 3.14 other ? 2.7 ----- -- ---- Various plain-text table formats (`tablefmt`) are supported: 'plain', 'simple', 'grid', 'pipe', 'orgtbl', 'rst', 'mediawiki', 'latex', 'latex_raw' and 'latex_booktabs'. Variable `tabulate_formats` contains the list of currently supported formats. "plain" format doesn't use any pseudographics to draw tables, it separates columns with a double space: >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], ... ["strings", "numbers"], "plain")) strings numbers spam 41.9999 eggs 451 >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="plain")) spam 41.9999 eggs 451 "simple" format is like Pandoc simple_tables: >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], ... ["strings", "numbers"], "simple")) strings numbers --------- --------- spam 41.9999 eggs 451 >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="simple")) ---- -------- spam 41.9999 eggs 451 ---- -------- "grid" is similar to tables produced by Emacs table.el package or Pandoc grid_tables: >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], ... ["strings", "numbers"], "grid")) +-----------+-----------+ | strings | numbers | +===========+===========+ | spam | 41.9999 | +-----------+-----------+ | eggs | 451 | +-----------+-----------+ >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="grid")) +------+----------+ | spam | 41.9999 | +------+----------+ | eggs | 451 | +------+----------+ "fancy_grid" draws a grid using box-drawing characters: >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], ... ["strings", "numbers"], "fancy_grid")) ╒═══════════╤═══════════╕ │ strings │ numbers │ ╞═══════════╪═══════════╡ │ spam │ 41.9999 │ ├───────────┼───────────┤ │ eggs │ 451 │ ╘═══════════╧═══════════╛ "pipe" is like tables in PHP Markdown Extra extension or Pandoc pipe_tables: >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], ... ["strings", "numbers"], "pipe")) | strings | numbers | |:----------|----------:| | spam | 41.9999 | | eggs | 451 | "presto" is like tables produce by the Presto CLI: >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], ... ["strings", "numbers"], "presto")) strings | numbers -----------+----------- spam | 41.9999 eggs | 451 >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="pipe")) |:-----|---------:| | spam | 41.9999 | | eggs | 451 | "orgtbl" is like tables in Emacs org-mode and orgtbl-mode. They are slightly different from "pipe" format by not using colons to define column alignment, and using a "+" sign to indicate line intersections: >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], ... ["strings", "numbers"], "orgtbl")) | strings | numbers | |-----------+-----------| | spam | 41.9999 | | eggs | 451 | >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="orgtbl")) | spam | 41.9999 | | eggs | 451 | "rst" is like a simple table format from reStructuredText; please note that reStructuredText accepts also "grid" tables: >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], ... ["strings", "numbers"], "rst")) ========= ========= strings numbers ========= ========= spam 41.9999 eggs 451 ========= ========= >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="rst")) ==== ======== spam 41.9999 eggs 451 ==== ======== "mediawiki" produces a table markup used in Wikipedia and on other MediaWiki-based sites: >>> print(tabulate([["strings", "numbers"], ["spam", 41.9999], ["eggs", "451.0"]], ... headers="firstrow", tablefmt="mediawiki")) {| class="wikitable" style="text-align: left;" |+ |- ! strings !! align="right"| numbers |- | spam || align="right"| 41.9999 |- | eggs || align="right"| 451 |} "html" produces HTML markup: >>> print(tabulate([["strings", "numbers"], ["spam", 41.9999], ["eggs", "451.0"]], ... headers="firstrow", tablefmt="html"))
strings numbers
spam 41.9999
eggs 451
"latex" produces a tabular environment of LaTeX document markup: >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="latex")) \begin{tabular}{lr} \hline spam & 41.9999 \\ eggs & 451 \\ \hline \end{tabular} "latex_raw" is similar to "latex", but doesn't escape special characters, such as backslash and underscore, so LaTeX commands may embedded into cells' values: >>> print(tabulate([["spam$_9$", 41.9999], ["\\emph{eggs}", "451.0"]], tablefmt="latex_raw")) \begin{tabular}{lr} \hline spam$_9$ & 41.9999 \\ \emph{eggs} & 451 \\ \hline \end{tabular} "latex_booktabs" produces a tabular environment of LaTeX document markup using the booktabs.sty package: >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="latex_booktabs")) \begin{tabular}{lr} \toprule spam & 41.9999 \\ eggs & 451 \\ \bottomrule \end{tabular} Number parsing -------------- By default, anything which can be parsed as a number is a number. This ensures numbers represented as strings are aligned properly. This can lead to weird results for particular strings such as specific git SHAs e.g. "42992e1" will be parsed into the number 429920 and aligned as such. To completely disable number parsing (and alignment), use `disable_numparse=True`. For more fine grained control, a list column indices is used to disable number parsing only on those columns e.g. `disable_numparse=[0, 2]` would disable number parsing only on the first and third columns. N)rrrycSsg|]}djtt|qS)ry)r9rerj)r4rsrrrr6sztabulate..TFcSsg|]\}}t||dqS))r)r)r4colnprrrr6scs,g|]$\}fdd|DqS)csg|]}t|qSr)r)r4rG)ctfl_fmtrmiss_vrrr6sz'tabulate...r)r4r@)r)rrrrr6scs g|]}|ttgkrnqSr)rr)r4r)numalignstralignrrr6scsg|]}|tqSr) MIN_PADDING)r4r)rrrr6src s$g|]\}}}t|||qSr)r)r4r@r5minw)rrrrrr6srcs,g|]$\}}t|tfdd|DqS)c3s|]}|VqdS)Nr)r4cl)rrrrH%sz&tabulate...)r)r4rr@)rrrr6%sc s(g|] \}}}t||||qSr)r)r4rr5r)rrrrr6&scs"g|]}tfdd|DqS)c3s|]}|VqdS)Nr)r4r)rrrrH*sz&tabulate...)r)r4r@)rrrr6*srz)rrtr9rerjrrrrrmultiline_formatsrr?rror _expand_numparserr8r basestringr_DEFAULT_FLOATFMT_DEFAULT_MISSINGVALrAssertionError enumerater _table_formats _format_table)rrrtablefmtrrrrrdisable_numparsecolalignZ list_of_listsZ plain_textcols numparsesZcoltypesZ float_formatsZ missing_valsZalignsidxr0Z minwidthsZt_colsZt_alignsrqr)rrrrrrrrsj          &   cCs>t|tr.dg|}x|D] }d||<qW|S| g|SdS)aB Return a list of bools of length `column_count` which indicates whether number parsing should be used on each column. If `disable_numparse` is a list of indices, each of those indices are False, and everything else is True. If `disable_numparse` is a bool, then the returned list is all the same. TFN)r r)rZ column_countrrrrrr3s     rcs*|r"d|fdd|D}|S|SdS)Nr>csg|]}|qSrr)r4rf)padrrr6Gsz_pad_row..r)cellsr' padded_cellsr)rr_pad_rowDs rcCs |\}}}||j||jS)z7Format row according to DataRow format without padding.)r9rB)rrhrrrrrrrgMs rgcCs,|sdSt|dr||||St||SdS)z5Return a string which represents a row of data cells.Nr)rrg)rr:r;rhrrr _build_rowSs   rcCs|jt|||||S)N)rpr)linesrr:r;rhrrr_append_basic_row]src sfdd|D}dd|Dtttfddt|DfddtD}x&|D]}t|} t|| |||qbW|S)Ncsg|]}|dqS)rr)r4r2)rrrr6csz)_append_multiline_row..cSsg|] }|jqSr)r)r4r@rrrr6dscs*g|]"\}}|d|gt|qS)r>)r)r4rr2)nlinesrrr6gscsg|]fddDqS)csg|] }|qSrr)r4r)irrr6hsz4_append_multiline_row...r)r4) cells_lines)rrr6hs)rrerr8rrr) rZpadded_multiline_cells padded_widthsr;rhrr:Z lines_cellslnZ padded_lnr)rrrr_append_multiline_rowbs  rcsN|sdSt|dr|||S|\}}}fdd|D}t||||fSdS)z3Return a string which represents a horizontal line.Nrcsg|] }|qSrr)r4r2)fillrrr6wsz_build_line..)rrg)r:r;linefmtrrrrr)r r _build_lineos   r"cCs|jt||||S)N)rpr")rr:r;r!rrr _append_line{sr#csg}|r|jr|jng}|j|j}fdd|D} |rPddttd} ntt} |} fdd|D} |jrd|krt|| ||j| r| || | |||j rd|krt|| ||j | o|j od |kr*x6| d dD]&} | || | ||j t|| ||j qW| || d| ||j n"x | D]} | || | ||j q0W|j rnd |krnt|| ||j |sz|rd j |SdSd S)z1Produce a plain-text representation of the table.csg|]}|dqS)rr)r4r2)rrrr6sz!_format_table..cSs|S)Nr)rsr\rrrrsz_format_table..)rcsg|]}|qSrr)r4rs)rpad_rowrrr6sr!r"r#Nr,r$rKrrr)r(r'r%r rrrr!r#r"r#r&r$r9)rrrrqr:r;rrZhiddenr%rZ append_rowZpadded_headersZ padded_rowsrsr)rr$rr s<    r c&Csddl}ddl}ddl}|jtj}y.|j|jdddddddd d d g\}}Wn>|jk r}z t|t||j d WYdd}~XnXg}t }d} d } d} d} x|D]\} }| d#krd}q| d$kr|} q| d%kr|}q| d&kr|j } q| d'kr.|t kr(td|t||j d|} q| d(kr>|} q| d)krt||j dqW|sj|j gn|}| dkr~|jnt| d!r}xj|D]b}|dkr|j }t|rt||| | ||| d"n*t|}t||| | ||| d"WdQRXqWWdQRXdS)*a Usage: tabulate [options] [FILE ...] Pretty-print tabular data. See also https://bitbucket.org/astanin/python-tabulate 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, grid, fancy_grid, pipe, orgtbl, rst, mediawiki, html, latex, latex_raw, latex_booktabs, tsv (default: simple) rNr,z h1o:s:F:A:f:helprQoutputzsep=zfloat=zalign=zformat=rrzz\s+r+-1--headerr-o--output-F--float-C --colalign-f--formatz"%s is not a supported table formatr-s--sep-h--helpr2)rrr rrr r)r'r()r)r*)r+r,)r-r.)r/r0)r1r2)r3r4)getoptsystextwrapdedent_main__doc__argv GetoptErrorrexitrrrstdinstdoutopenr _pprint_file)r5r6r7usageoptsargsrrrrrr routfileoptvaluefilesoutr Zfobjrrrr9sh              r9c s8|j}fdd|D}tt|||||d|ddS)Ncs$g|]}|jrtj|jqSr)rlrrrB)r4rT)rrrr6sz _pprint_file..)rr)r ) readlinesrr) Zfobjectrrr rrr rrqtabler)rrrAs rA__main__)rrr)r)F)TT)rTFF)TT)rT)FN)r)mr: __future__rr collectionsrplatformrrrcollections.abcr itertoolsr functoolsr rrrrrZ _int_typelongrrZ _float_typeunicoderjstrrkrrrbytesrrr ImportError__all__ __version__rrrrrrrr r3r=rFrJrLrPrRrWZLATEX_ESCAPE_RULESrirtr rosortedrrrcompilerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrgrrrr"r#r r9rA__name__rrrrs              "                                                                     &     "  "   " e    .N