from textwrap import dedent
import pytablewriter
table_name = "example_table"
headers = ["int", "float", "str", "bool", "mix", "time"]
data = [
[0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"],
[2, "-2.23", "foo", False, None, "2017-12-23 12:34:51+0900"],
[3, 0, "bar", "true", "inf", "2017-03-03 22:44:55+0900"],
[-10, -9.9, "", "FALSE", "nan", "2017-01-01 00:00:00+0900"],
]
for name in pytablewriter.TableWriterFactory.get_format_names():
print(name)
adoc asciidoc bold_unicode borderless css csv elasticsearch excel htm html javascript js json json_lines jsonl latex_matrix latex_table ldjson ltsv markdown md mediawiki ndjson null numpy pandas pandas_pickle py python rst rst_csv rst_csv_table rst_grid rst_grid_table rst_simple rst_simple_table space_aligned sqlite ssv toml tsv unicode yaml
for name in pytablewriter.TableWriterFactory.get_extensions():
print(name)
adoc asc asciidoc css csv htm html js json jsonl ldjson ltsv md ndjson py rst sqlite sqlite3 tex toml tsv xls xlsx yml
from pytablewriter import MarkdownTableWriter
writer = MarkdownTableWriter()
writer.table_name = "zone"
writer.headers = ["zone_id", "country_code", "zone_name"]
writer.value_matrix = [
["1", "AD", "Europe/Andorra"],
["2", "AE", "Asia/Dubai"],
["3", "AF", "Asia/Kabul"],
["4", "AG", "America/Antigua"],
["5", "AI", "America/Anguilla"],
]
writer.write_table()
# zone |zone_id|country_code| zone_name | |------:|------------|----------------| | 1|AD |Europe/Andorra | | 2|AE |Asia/Dubai | | 3|AF |Asia/Kabul | | 4|AG |America/Antigua | | 5|AI |America/Anguilla|
from pytablewriter import MarkdownTableWriter
from pytablewriter.style import Style
writer = MarkdownTableWriter(
table_name="Markdown table with GitHub flavor",
column_styles=[
Style(fg_color="red"),
Style(fg_color="green", decoration_line="underline"),
],
headers=["A", "B"],
value_matrix=[
["abc", 1],
["efg", 2],
],
margin=1,
flavor="github",
enable_ansi_escape=False,
)
writer.write_table()
# Markdown table with GitHub flavor | A | B | | --------------------------------- | --------------------------------------------: | | $$\textcolor{#cd3131}{\tt{abc}}$$ | $$\textcolor{#0dbc79}{\underline{\tt{1}}}$$ | | $$\textcolor{#cd3131}{\tt{efg}}$$ | $$\textcolor{#0dbc79}{\underline{\tt{2}}}$$ |
from typing import Any, Optional
from pytablewriter import MarkdownTableWriter
from pytablewriter.style import Cell, Style
def style_filter(cell: Cell, **kwargs: Any) -> Optional[Style]:
if cell.is_header_row():
return None
if cell.col == 0:
return Style(font_weight="bold")
value = int(cell.value)
if value > 80:
return Style(fg_color="red", font_weight="bold", decoration_line="underline")
elif value > 50:
return Style(fg_color="yellow", font_weight="bold")
elif value > 20:
return Style(fg_color="green")
return Style(fg_color="lightblue")
writer = MarkdownTableWriter(
table_name="style filter example",
headers=["Key", "Value 1", "Value 2"],
value_matrix=[
["A", 95, 40],
["B", 55, 5],
["C", 30, 85],
["D", 0, 69],
],
flavor="github",
enable_ansi_escape=False,
)
writer.add_style_filter(style_filter)
writer.write_table()
# style filter example |Key|Value 1|Value 2| |---|------:|------:| |**A**|$$\textcolor{#cd3131}{\underline{\tt{\bf{95}}}}$$|$$\textcolor{#0dbc79}{\tt{40}}$$| |**B**|$$\textcolor{#e5e510}{\tt{\bf{55}}}$$|$$\textcolor{#3b8eea}{\tt{5}}$$| |**C**|$$\textcolor{#0dbc79}{\tt{30}}$$|$$\textcolor{#cd3131}{\underline{\tt{\bf{85}}}}$$| |**D**|$$\textcolor{#3b8eea}{\tt{0}}$$|$$\textcolor{#e5e510}{\tt{\bf{69}}}$$|
writer = pytablewriter.CsvTableWriter()
writer.headers = headers
writer.value_matrix = data
writer.write_table()
"int","float","str","bool","mix","time" 0,0.1,"hoge",True,0,"2017-01-01 03:04:05+0900" 2,-2.23,"foo",False,,"2017-12-23 12:34:51+0900" 3,0,"bar",True,Infinity,"2017-03-03 22:44:55+0900" -10,-9.9,,False,NaN,"2017-01-01 00:00:00+0900"
writer = pytablewriter.SpaceAlignedTableWriter()
writer.headers = [
"PID",
"USER",
"PR",
"NI",
"VIRT",
"RES",
"SHR",
"S",
"%CPU",
"%MEM",
"TIME+",
"COMMAND",
]
writer.value_matrix = csv1 = [
[32866, "root", 20, 0, 48344, 3924, 3448, "R", 5.6, 0.2, "0:00.03", "top"],
[1, "root", 20, 0, 212080, 7676, 5876, "S", 0, 0.4, "1:06.56", "systemd"],
[2, "root", 20, 0, 0, 0, 0, "S", 0, 0, "0:01.92", "kthreadd"],
[4, "root", 0, -20, 0, 0, 0, "S", 0, 0, "0:00.00", "kworker/0:0H"],
]
writer.write_table()
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 32866 root 20 0 48344 3924 3448 R 5.6 0.2 0:00.03 top 1 root 20 0 212080 7676 5876 S 0.0 0.4 1:06.56 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:01.92 kthreadd 4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
writer = pytablewriter.TsvTableWriter()
writer.headers = headers
writer.value_matrix = data
writer.write_table()
"int" "float" "str" "bool" "mix" "time" 0 0.1 "hoge" True 0 "2017-01-01 03:04:05+0900" 2 -2.23 "foo" False "2017-12-23 12:34:51+0900" 3 0 "bar" True Infinity "2017-03-03 22:44:55+0900" -10 -9.9 False NaN "2017-01-01 00:00:00+0900"
writer = pytablewriter.HtmlTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
<table id="example_table"> <caption>example_table</caption> <thead> <tr> <th>int</th> <th>float</th> <th>str</th> <th>bool</th> <th>mix</th> <th>time</th> </tr> </thead> <tbody> <tr> <td align="right">0</td> <td align="right">0.10</td> <td align="left">hoge</td> <td align="left">True</td> <td align="right">0</td> <td align="left">2017-01-01 03:04:05+0900</td> </tr> <tr> <td align="right">2</td> <td align="right">-2.23</td> <td align="left">foo</td> <td align="left">False</td> <td align="right"></td> <td align="left">2017-12-23 12:34:51+0900</td> </tr> <tr> <td align="right">3</td> <td align="right">0.00</td> <td align="left">bar</td> <td align="left">True</td> <td align="right">Infinity</td> <td align="left">2017-03-03 22:44:55+0900</td> </tr> <tr> <td align="right">-10</td> <td align="right">-9.90</td> <td align="left"></td> <td align="left">False</td> <td align="right">NaN</td> <td align="left">2017-01-01 00:00:00+0900</td> </tr> </tbody> </table>
writer = pytablewriter.JavaScriptTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
const example_table = [ ["int", "float", "str", "bool", "mix", "time"], [0, 0.1, "hoge", true, 0, "2017-01-01 03:04:05+0900"], [2, -2.23, "foo", false, null, "2017-12-23 12:34:51+0900"], [3, 0, "bar", true, Infinity, "2017-03-03 22:44:55+0900"], [-10, -9.9, "", "FALSE", NaN, "2017-01-01 00:00:00+0900"] ];
writer = pytablewriter.JsonTableWriter()
# writer.table_name = "Timezone"
writer.headers = headers
writer.value_matrix = data
writer.write_table()
[ { "int": 0, "float": 0.1, "str": "hoge", "bool": true, "mix": 0, "time": "2017-01-01 03:04:05+0900" }, { "int": 2, "float": -2.23, "str": "foo", "bool": false, "mix": null, "time": "2017-12-23 12:34:51+0900" }, { "int": 3, "float": 0, "str": "bar", "bool": "true", "mix": "Infinity", "time": "2017-03-03 22:44:55+0900" }, { "int": -10, "float": -9.9, "str": "", "bool": "FALSE", "mix": "NaN", "time": "2017-01-01 00:00:00+0900" } ]
writer = pytablewriter.JsonLinesTableWriter()
writer.headers = headers
writer.value_matrix = data
writer.write_table()
{"int": 0, "float": 0.1, "str": "hoge", "bool": true, "mix": 0, "time": "2017-01-01 03:04:05+0900"} {"int": 2, "float": -2.23, "str": "foo", "bool": false, "mix": null, "time": "2017-12-23 12:34:51+0900"} {"int": 3, "float": 0, "str": "bar", "bool": "true", "mix": "Infinity", "time": "2017-03-03 22:44:55+0900"} {"int": -10, "float": -9.9, "str": "", "bool": "FALSE", "mix": "NaN", "time": "2017-01-01 00:00:00+0900"}
writer = pytablewriter.JsonTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
{ "example_table" : [ { "int": 0, "float": 0.1, "str": "hoge", "bool": true, "mix": 0, "time": "2017-01-01 03:04:05+0900" }, { "int": 2, "float": -2.23, "str": "foo", "bool": false, "mix": null, "time": "2017-12-23 12:34:51+0900" }, { "int": 3, "float": 0, "str": "bar", "bool": "true", "mix": "Infinity", "time": "2017-03-03 22:44:55+0900" }, { "int": -10, "float": -9.9, "str": "", "bool": "FALSE", "mix": "NaN", "time": "2017-01-01 00:00:00+0900" } ]}
writer = pytablewriter.LatexMatrixWriter()
writer.table_name = "A"
writer.value_matrix = [
[0.01, 0.00125, 0.0],
[1.0, 99.9, 0.01],
[1.2, 999999.123, 0.001],
]
writer.write_table()
\begin{equation} A = \left( \begin{array}{rrr} 0.01 & 0.00125 & 0.000 \\ 1.00 & 99.90000 & 0.010 \\ 1.20 & 999999.12300 & 0.001 \\ \end{array} \right) \end{equation}
writer = pytablewriter.LatexMatrixWriter()
writer.table_name = "B"
writer.value_matrix = [
["a_{11}", "a_{12}", "\\ldots", "a_{1n}"],
["a_{21}", "a_{22}", "\\ldots", "a_{2n}"],
[r"\vdots", "\\vdots", "\\ddots", "\\vdots"],
["a_{n1}", "a_{n2}", "\\ldots", "a_{nn}"],
]
writer.write_table()
\begin{equation} B = \left( \begin{array}{llll} a_{11} & a_{12} & \ldots & a_{1n} \\ a_{21} & a_{22} & \ldots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n1} & a_{n2} & \ldots & a_{nn} \\ \end{array} \right) \end{equation}
writer = pytablewriter.LatexTableWriter()
writer.headers = headers
writer.value_matrix = data
writer.write_table()
\begin{array}{r | r | l | l | l | l} \hline \verb|int| & \verb|float| & \verb|str | & \verb|bool | & \verb| mix | & \verb| time | \\ \hline \hline 0 & 0.10 & hoge & True & 0 & \verb|2017-01-01 03:04:05+0900| \\ \hline 2 & -2.23 & foo & False & & \verb|2017-12-23 12:34:51+0900| \\ \hline 3 & 0.00 & bar & True & \infty & \verb|2017-03-03 22:44:55+0900| \\ \hline -10 & -9.90 & & False & NaN & \verb|2017-01-01 00:00:00+0900| \\ \hline \end{array}
from pytablewriter import MarkdownTableWriter
writer = MarkdownTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
# example_table |int|float|str |bool | mix | time | |--:|----:|----|-----|-------:|------------------------| | 0| 0.10|hoge|True | 0|2017-01-01 03:04:05+0900| | 2|-2.23|foo |False| |2017-12-23 12:34:51+0900| | 3| 0.00|bar |True |Infinity|2017-03-03 22:44:55+0900| |-10|-9.90| |False| NaN|2017-01-01 00:00:00+0900|
from pytablewriter import MarkdownTableWriter
writer = MarkdownTableWriter(
table_name="write example with margin=1",
headers=headers,
value_matrix=data,
margin=1, # add a whitespace for both sides of each cell
)
writer.write_table()
# write example with margin=1 | int | float | str | bool | mix | time | | --: | ----: | ---- | ----- | -------: | ------------------------ | | 0 | 0.10 | hoge | True | 0 | 2017-01-01 03:04:05+0900 | | 2 | -2.23 | foo | False | | 2017-12-23 12:34:51+0900 | | 3 | 0.00 | bar | True | Infinity | 2017-03-03 22:44:55+0900 | | -10 | -9.90 | | False | NaN | 2017-01-01 00:00:00+0900 |
writer = pytablewriter.MediaWikiTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
{| class="wikitable" |+example_table ! int ! float ! str ! bool ! mix ! time |- | style="text-align:right"| 0 | style="text-align:right"| 0.10 | hoge | True | style="text-align:right"| 0 | 2017-01-01 03:04:05+0900 |- | style="text-align:right"| 2 | style="text-align:right"| -2.23 | foo | False | | 2017-12-23 12:34:51+0900 |- | style="text-align:right"| 3 | style="text-align:right"| 0.00 | bar | True | Infinity | 2017-03-03 22:44:55+0900 |- | style="text-align:right"| -10 | style="text-align:right"| -9.90 | | False | NaN | 2017-01-01 00:00:00+0900 |}
writer = pytablewriter.NumpyTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
example_table = np.array([ ["int", "float", "str", "bool", "mix", "time"], [0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"], [2, -2.23, "foo", False, None, "2017-12-23 12:34:51+0900"], [3, 0, "bar", True, np.inf, "2017-03-03 22:44:55+0900"], [-10, -9.9, "", False, np.nan, "2017-01-01 00:00:00+0900"], ])
writer = pytablewriter.PandasDataFrameWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
example_table = pd.DataFrame([ [0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"], [2, -2.23, "foo", False, None, "2017-12-23 12:34:51+0900"], [3, 0, "bar", True, np.inf, "2017-03-03 22:44:55+0900"], [-10, -9.9, "", False, np.nan, "2017-01-01 00:00:00+0900"], ], columns=["int", "float", "str", "bool", "mix", "time"])
writer = pytablewriter.PandasDataFrameWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.is_datetime_instance_formatting = False
writer.write_table()
example_table = pd.DataFrame([ [0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"], [2, -2.23, "foo", False, None, "2017-12-23 12:34:51+0900"], [3, 0, "bar", True, np.inf, "2017-03-03 22:44:55+0900"], [-10, -9.9, "", False, np.nan, "2017-01-01 00:00:00+0900"], ], columns=["int", "float", "str", "bool", "mix", "time"])
writer = pytablewriter.PythonCodeTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
example_table = [ ["int", "float", "str", "bool", "mix", "time"], [0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"], [2, -2.23, "foo", False, None, "2017-12-23 12:34:51+0900"], [3, 0, "bar", True, float("inf"), "2017-03-03 22:44:55+0900"], [-10, -9.9, "", False, float("nan"), "2017-01-01 00:00:00+0900"], ]
writer = pytablewriter.PythonCodeTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.is_datetime_instance_formatting = False
writer.write_table()
example_table = [ ["int", "float", "str", "bool", "mix", "time"], [0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"], [2, -2.23, "foo", False, None, "2017-12-23 12:34:51+0900"], [3, 0, "bar", True, float("inf"), "2017-03-03 22:44:55+0900"], [-10, -9.9, "", False, float("nan"), "2017-01-01 00:00:00+0900"], ]
writer = pytablewriter.RstGridTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
.. table:: example_table +---+-----+----+-----+--------+------------------------+ |int|float|str |bool | mix | time | +===+=====+====+=====+========+========================+ | 0| 0.10|hoge|True | 0|2017-01-01 03:04:05+0900| +---+-----+----+-----+--------+------------------------+ | 2|-2.23|foo |False| |2017-12-23 12:34:51+0900| +---+-----+----+-----+--------+------------------------+ | 3| 0.00|bar |True |Infinity|2017-03-03 22:44:55+0900| +---+-----+----+-----+--------+------------------------+ |-10|-9.90| |False| NaN|2017-01-01 00:00:00+0900| +---+-----+----+-----+--------+------------------------+
writer = pytablewriter.RstSimpleTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
.. table:: example_table === ===== ==== ===== ======== ======================== int float str bool mix time === ===== ==== ===== ======== ======================== 0 0.10 hoge True 0 2017-01-01 03:04:05+0900 2 -2.23 foo False 2017-12-23 12:34:51+0900 3 0.00 bar True Infinity 2017-03-03 22:44:55+0900 -10 -9.90 False NaN 2017-01-01 00:00:00+0900 === ===== ==== ===== ======== ========================
writer = pytablewriter.RstCsvTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
.. csv-table:: example_table :header: "int", "float", "str", "bool", "mix", "time" :widths: 5, 7, 6, 6, 8, 26 0, 0.10, "hoge", True, 0, "2017-01-01 03:04:05+0900" 2, -2.23, "foo", False, , "2017-12-23 12:34:51+0900" 3, 0.00, "bar", True, Infinity, "2017-03-03 22:44:55+0900" -10, -9.90, , False, NaN, "2017-01-01 00:00:00+0900"
writer = pytablewriter.LtsvTableWriter()
writer.headers = headers
writer.value_matrix = data
writer.write_table()
int:0 float:0.1 str:"hoge" bool:True mix:0 time:"2017-01-01 03:04:05+0900" int:2 float:-2.23 str:"foo" bool:False time:"2017-12-23 12:34:51+0900" int:3 float:0 str:"bar" bool:True mix:Infinity time:"2017-03-03 22:44:55+0900" int:-10 float:-9.9 bool:False mix:NaN time:"2017-01-01 00:00:00+0900"
writer = pytablewriter.TomlTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
[[example_table]] int = 0 float = 0.1 str = "hoge" bool = true mix = 0 time = "2017-01-01 03:04:05+0900" [[example_table]] int = 2 float = -2.23 str = "foo" bool = false time = "2017-12-23 12:34:51+0900" [[example_table]] int = 3 float = 0 str = "bar" bool = true mix = inf time = "2017-03-03 22:44:55+0900" [[example_table]] int = -10 float = -9.9 str = "" bool = false mix = nan time = "2017-01-01 00:00:00+0900"
from datetime import datetime
from pytablewriter import JavaScriptTableWriter
def main():
writer = JavaScriptTableWriter()
writer.headers = ["header_a", "header_b", "header_c"]
writer.value_matrix = [
[-1.1, "2017-01-02 03:04:05", datetime(2017, 1, 2, 3, 4, 5)],
[0.12, "2017-02-03 04:05:06", datetime(2017, 2, 3, 4, 5, 6)],
]
print("// without type hints: column data types detected automatically by default")
writer.table_name = "without type hint"
writer.write_table()
print("\n// with type hints")
writer.table_name = "with type hint"
writer.type_hints = ["int", "datetime", "str"]
writer.write_table()
if __name__ == "__main__":
main()
// without type hints: column data types detected automatically by default const without_type_hint = [ ["header_a", "header_b", "header_c"], [-1.1, "2017-01-02 03:04:05", new Date("2017-01-02T03:04:05")], [0.12, "2017-02-03 04:05:06", new Date("2017-02-03T04:05:06")] ]; // with type hints const with_type_hint = [ ["header_a", "header_b", "header_c"], [-1, new Date("2017-01-02T03:04:05"), "2017-01-02 03:04:05"], [0, new Date("2017-02-03T04:05:06"), "2017-02-03 04:05:06"] ];
from datetime import datetime
from pytablewriter import PythonCodeTableWriter
def main():
writer = PythonCodeTableWriter()
writer.value_matrix = [
[-1.1, float("inf"), "2017-01-02 03:04:05", datetime(2017, 1, 2, 3, 4, 5)],
[0.12, float("nan"), "2017-02-03 04:05:06", datetime(2017, 2, 3, 4, 5, 6)],
]
# column data types detected automatically by default
writer.table_name = "python variable without type hints"
writer.headers = ["float", "infnan", "string", "datetime"]
writer.write_table()
# set type hints
writer.table_name = "python variable with type hints"
writer.headers = ["hint_int", "hint_str", "hint_datetime", "hint_str"]
writer.type_hints = ["int", "datetime", "str"]
writer.write_table()
if __name__ == "__main__":
main()
python_variable_without_type_hints = [ ["float", "infnan", "string", "datetime"], [-1.1, float("inf"), "2017-01-02 03:04:05", dateutil.parser.parse("2017-01-02T03:04:05")], [0.12, float("nan"), "2017-02-03 04:05:06", dateutil.parser.parse("2017-02-03T04:05:06")], ] python_variable_with_type_hints = [ ["hint_int", "hint_str", "hint_datetime", "hint_str"], [-1, float("inf"), "2017-01-02 03:04:05", dateutil.parser.parse("2017-01-02T03:04:05")], [0, float("nan"), "2017-02-03 04:05:06", dateutil.parser.parse("2017-02-03T04:05:06")], ]
writer = pytablewriter.MarkdownTableWriter()
writer.from_csv(
dedent(
"""\
"i","f","c","if","ifc","bool","inf","nan","mix_num","time"
1,1.10,"aa",1.0,"1",True,Infinity,NaN,1,"2017-01-01 00:00:00+09:00"
2,2.20,"bbb",2.2,"2.2",False,Infinity,NaN,Infinity,"2017-01-02 03:04:05+09:00"
3,3.33,"cccc",-3.0,"ccc",True,Infinity,NaN,NaN,"2017-01-01 00:00:00+09:00"
"""
)
)
writer.write_table()
| i | f | c | if |ifc|bool | inf |nan|mix_num | time | |--:|---:|----|---:|---|-----|--------|---|-------:|-------------------------| | 1|1.10|aa | 1.0| 1|True |Infinity|NaN| 1|2017-01-01 00:00:00+09:00| | 2|2.20|bbb | 2.2|2.2|False|Infinity|NaN|Infinity|2017-01-02 03:04:05+09:00| | 3|3.33|cccc|-3.0|ccc|True |Infinity|NaN| NaN|2017-01-01 00:00:00+09:00|
writer = pytablewriter.MarkdownTableWriter()
writer.table_name = "ps"
writer.from_csv(
dedent(
"""\
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.4 77664 8784 ? Ss May11 0:02 /sbin/init
root 2 0.0 0.0 0 0 ? S May11 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? I< May11 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? I< May11 0:00 [mm_percpu_wq]
root 7 0.0 0.0 0 0 ? S May11 0:01 [ksoftirqd/0]
"""
),
delimiter=" ",
)
writer.write_table()
# ps |USER|PID|%CPU|%MEM| VSZ |RSS |TTY|STAT|START|TIME| COMMAND | |----|--:|---:|---:|----:|---:|---|----|-----|----|--------------| |root| 1| 0| 0.4|77664|8784|? |Ss |May11|0:02|/sbin/init | |root| 2| 0| 0.0| 0| 0|? |S |May11|0:00|[kthreadd] | |root| 4| 0| 0.0| 0| 0|? |I< |May11|0:00|[kworker/0:0H]| |root| 6| 0| 0.0| 0| 0|? |I< |May11|0:00|[mm_percpu_wq]| |root| 7| 0| 0.0| 0| 0|? |S |May11|0:01|[ksoftirqd/0] |
from pytablewriter import MarkdownTableWriter
from pytablewriter.style import Align, Style
writer = MarkdownTableWriter()
writer.table_name = "specify alignment for each column manually"
writer.headers = ["left", "right", "center", "auto (int)", "auto (str)", "None (same as AUTO)"]
writer.value_matrix = [
[0, "r", "center align", 0, "a", "n"],
[11, "right align", "c", 11, "auto", "none"],
]
# set alignments for each column
writer.column_styles = [
Style(align=Align.LEFT),
Style(align=Align.RIGHT),
Style(align=Align.CENTER),
Style(align=Align.AUTO),
Style(align=Align.AUTO),
None,
]
writer.write_table()
# specify alignment for each column manually |left| right | center |auto (int)|auto (str)|None (same as AUTO)| |----|----------:|:----------:|---------:|----------|-------------------| |0 | r|center align| 0|a |n | |11 |right align| c | 11|auto |none |
from pytablewriter import MarkdownTableWriter
from pytablewriter.style import Style
writer = MarkdownTableWriter()
writer.table_name = "set style by styles"
writer.headers = [
"auto align",
"left align",
"center align",
"bold",
"italic",
"bold italic ts",
]
writer.value_matrix = [
[11, 11, 11, 11, 11, 11],
[1234, 1234, 1234, 1234, 1234, 1234],
]
# specify styles for each column
writer.column_styles = [
Style(),
Style(align="left"),
Style(align="center"),
Style(font_weight="bold"),
Style(font_style="italic"),
Style(font_weight="bold", font_style="italic", thousand_separator=","),
]
writer.write_table()
# set style by styles |auto align|left align|center align| bold |italic|bold italic ts| |---------:|----------|:----------:|-------:|-----:|-------------:| | 11|11 | 11 | **11**| _11_| _**11**_| | 1234|1234 | 1234 |**1234**|_1234_| _**1,234**_|
from pytablewriter import MarkdownTableWriter
from pytablewriter.style import Style
writer = MarkdownTableWriter()
writer.headers = [
"A",
"B",
"C",
]
writer.value_matrix = [[11, 11, 11], [1234, 1234, 1234]]
writer.table_name = "set style by column index"
writer.set_style(1, Style(align="center", font_weight="bold"))
writer.set_style(2, Style(thousand_separator=" "))
writer.write_table()
writer.write_null_line()
writer.table_name = "set style by header"
writer.set_style("B", Style(font_style="italic"))
writer.write_table()
# set style by column index | A | B | C | |---:|:------:|----:| | 11| **11** | 11| |1234|**1234**|1 234| # set style by header | A | B | C | |---:|-----:|----:| | 11| _11_| 11| |1234|_1234_|1 234|
import pytablewriter
writer = pytablewriter.MarkdownTableWriter()
writer.headers = ["int", "float", "str", "bool", "mix", "time"]
writer.value_matrix = [
[0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"],
[2, "-2.23", "foo", False, None, "2017-12-23 45:01:23+0900"],
[3, 0, "bar", "true", "inf", "2017-03-03 33:44:55+0900"],
[-10, -9.9, "", "FALSE", "nan", "2017-01-01 00:00:00+0900"],
]
print(writer.dumps())
|int|float|str |bool | mix | time | |--:|----:|----|-----|-------:|------------------------| | 0| 0.10|hoge|True | 0|2017-01-01 03:04:05+0900| | 2|-2.23|foo |False| |2017-12-23 45:01:23+0900| | 3| 0.00|bar |True |Infinity|2017-03-03 33:44:55+0900| |-10|-9.90| |False| NaN|2017-01-01 00:00:00+0900|
from pytablewriter import MarkdownTableWriter
from pytablewriter.style import ThousandSeparator
writer = MarkdownTableWriter()
writer.headers = ["wo_format", "comma_i", "space_f"]
writer.value_matrix = [
[1000, 1234567, 1234567.8],
[1000, 1234567, 1234567.8],
[1000, 1234567, 1234567.8],
]
writer.column_styles = [
Style(thousand_separator=ThousandSeparator.NONE),
Style(thousand_separator=ThousandSeparator.COMMA),
Style(thousand_separator=ThousandSeparator.SPACE),
]
writer.write_table()
|wo_format| comma_i | space_f | |--------:|--------:|----------:| | 1000|1,234,567|1 234 567.8| | 1000|1,234,567|1 234 567.8| | 1000|1,234,567|1 234 567.8|
from pytablewriter import LatexTableWriter
from pytablewriter.style import Style, FontSize
writer = LatexTableWriter()
writer.table_name = "style test: font size"
writer.headers = ["none", "empty_style", "tiny", "small", "medium", "large"]
writer.value_matrix = [[111, 111, 111, 111, 111, 111], [1234, 1234, 1234, 1234, 1234, 1234]]
writer.column_styles = [
None,
Style(),
Style(font_size=FontSize.TINY),
Style(font_size=FontSize.SMALL),
Style(font_size=FontSize.MEDIUM),
Style(font_size=FontSize.LARGE),
]
writer.write_table()
\begin{array}{r | r | r | r | r | r} \hline \verb|none| & \verb|empty_style| & \verb| tiny | & \verb| small | & \verb| medium | & \verb| large | \\ \hline \hline 111 & 111 & \tiny{111} & \small{111} & \normalsize{111} & \large{111} \\ \hline 1234 & 1234 & \tiny{1234} & \small{1234} & \normalsize{1234} & \large{1234} \\ \hline \end{array}
from pytablewriter import UnicodeTableWriter
writer = UnicodeTableWriter()
writer.table_name = table_name
writer.headers = headers
writer.value_matrix = data
writer.write_table()
┌───┬─────┬────┬─────┬────────┬────────────────────────┐ │int│float│str │bool │ mix │ time │ ├───┼─────┼────┼─────┼────────┼────────────────────────┤ │ 0│ 0.10│hoge│True │ 0│2017-01-01 03:04:05+0900│ ├───┼─────┼────┼─────┼────────┼────────────────────────┤ │ 2│-2.23│foo │False│ │2017-12-23 12:34:51+0900│ ├───┼─────┼────┼─────┼────────┼────────────────────────┤ │ 3│ 0.00│bar │True │Infinity│2017-03-03 22:44:55+0900│ ├───┼─────┼────┼─────┼────────┼────────────────────────┤ │-10│-9.90│ │False│ NaN│2017-01-01 00:00:00+0900│ └───┴─────┴────┴─────┴────────┴────────────────────────┘
from textwrap import dedent
import pandas as pd
import io
from pytablewriter import MarkdownTableWriter
def main():
csv_data = io.StringIO(
dedent(
"""\
"i","f","c","if","ifc","bool","inf","nan","mix_num","time"
1,1.10,"aa",1.0,"1",True,Infinity,NaN,1,"2017-01-01 00:00:00+09:00"
2,2.20,"bbb",2.2,"2.2",False,Infinity,NaN,Infinity,"2017-01-02 03:04:05+09:00"
3,3.33,"cccc",-3.0,"ccc",True,Infinity,NaN,NaN,"2017-01-01 00:00:00+09:00"
"""
)
)
df = pd.read_csv(csv_data, sep=",")
writer = MarkdownTableWriter(dataframe=df)
writer.write_table()
if __name__ == "__main__":
main()
| i | f | c | if |ifc|bool | inf |nan|mix_num | time | |--:|---:|----|---:|---|-----|--------|---|-------:|-------------------------| | 1|1.10|aa | 1.0| 1|True |Infinity|NaN| 1|2017-01-01 00:00:00+09:00| | 2|2.20|bbb | 2.2|2.2|False|Infinity|NaN|Infinity|2017-01-02 03:04:05+09:00| | 3|3.33|cccc|-3.0|ccc|True |Infinity|NaN| NaN|2017-01-01 00:00:00+09:00|