Skip to content

Latest commit

 

History

History
478 lines (391 loc) · 17.6 KB

README.rst

File metadata and controls

478 lines (391 loc) · 17.6 KB

pytablewriter

Table of Contents

Summary

A Python library to write a table in various formats: CSV / Elasticsearch / HTML / JavaScript / JSON / Jupyter Notebook / LaTeX / LTSV / Markdown / MediaWiki / NumPy / Excel / Pandas / Python / reStructuredText / SQLite / TOML / TSV.

image

image

image

image

image

Features

  • Write a table in various formats:
  • Automatic tabular data formatting
    • Alignment
    • Padding
    • Decimal places of numbers
  • Multibyte character support
  • Write table to a stream such as a file/standard-output/string-buffer

Examples

Write a Markdown table

Sample Code
import pytablewriter

writer = pytablewriter.MarkdownTableWriter()
writer.table_name = "example_table"
writer.header_list = ["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"],
]

writer.write_table()
Output
# 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|
Rendering Result
Rendered markdown at GitHubRendered markdown at GitHub

Write a Markdown table with a margin

Sample Code
import pytablewriter

writer = pytablewriter.MarkdownTableWriter()
writer.table_name = "write example with a margin"
writer.header_list = ["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"],
]
writer.margin = 1  # add a whitespace for both sides of each cell

writer.write_table()
Output
# write example with a margin
| 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 |

margin attribute can be available for all of the text format writer classes.

Write a reStructuredText table (Grid Tables)

Sample Code
import pytablewriter

writer = pytablewriter.RstGridTableWriter()
writer.table_name = "example_table"
writer.header_list = ["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"],
]

writer.write_table()
Output
.. 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|
    +---+-----+----+-----+--------+------------------------+
Rendering Result
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

Write a table with JavaScript format (as a nested list variable definition)

Sample Code
import pytablewriter

writer = pytablewriter.JavaScriptTableWriter()
writer.table_name = "example_table"
writer.header_list = ["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"],
]

writer.write_table()
Output
const 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, null, "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"]
];

Write a table to an Excel sheet

Sample Code
import pytablewriter

writer = pytablewriter.ExcelXlsxTableWriter()
writer.open("sample.xlsx")

writer.make_worksheet("example")
writer.header_list = ["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 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"],
]
writer.write_table()

writer.close()
Output
Output excel file (sample_single.xlsx)Output excel file (sample_single.xlsx)

Write a Markdown table from pandas.DataFrame instance

Sample Code
import pandas as pd
import pytablewriter
from StringIO import StringIO

csv_data = StringIO(u""""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 = pytablewriter.MarkdownTableWriter()
writer.from_dataframe(df)
writer.write_table()
Output
i | f  | c  | if |ifc|bool |  inf   |nan|mix_num |          time
--:----------------------:1.10 1.0True NaN| 12.20 2.2FalseNaN2017-01-02 03:04:05+09:00

3cccccccInfinity NaN|2017-01-01 00:00:00+09:00

Create Elasticsearch index and put data

Sample Code
import datetime
import json

from elasticsearch import Elasticsearch
import pytablewriter as ptw

es = Elasticsearch(hosts="localhost:9200")

writer = ptw.ElasticsearchWriter()
writer.stream = es
writer.index_name = "es writer example"
writer.header_list = [
    "str", "byte", "short", "int", "long", "float", "date", "bool", "ip",
]
writer.value_matrix = [
    [
        "abc", 100, 10000, 2000000000, 200000000000, 0.1,
        datetime.datetime(2017, 1, 2, 3, 4, 5), True, "127.0.0.1",
    ],
    [
        "def", -10, -1000, -200000000, -20000000000, 100.1,
        datetime.datetime(2017, 6, 5, 4, 5, 2), False, "::1",
    ],
]

# delete existing index ---
es.indices.delete(index=writer.index_name, ignore=404)

# create an index and put data ---
writer.write_table()

# display the result ---
es.indices.refresh(index=writer.index_name)

print("----- mappings -----")
response = es.indices.get_mapping(index=writer.index_name, doc_type="table")
print("{}\n".format(json.dumps(response, indent=4)))

print("----- documents -----")
response = es.search(
    index=writer.index_name,
    doc_type="table",
    body={
        "query": {"match_all": {}}
    }
)
for hit in response["hits"]["hits"]:
    print(json.dumps(hit["_source"], indent=4))
Output
----- mappings -----
{
    "es_writer_example": {
        "mappings": {
            "table": {
                "properties": {
                    "bool": {
                        "type": "boolean"
                    },
                    "byte": {
                        "type": "byte"
                    },
                    "date": {
                        "type": "date",
                        "format": "date_optional_time"
                    },
                    "float": {
                        "type": "double"
                    },
                    "int": {
                        "type": "integer"
                    },
                    "ip": {
                        "type": "text"
                    },
                    "long": {
                        "type": "long"
                    },
                    "short": {
                        "type": "short"
                    },
                    "str": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

----- documents -----
{
    "str": "def",
    "byte": -10,
    "short": -1000,
    "int": -200000000,
    "long": -20000000000,
    "float": 100.1,
    "date": "2017-06-05T04:05:02",
    "bool": false,
    "ip": "::1"
}
{
    "str": "abc",
    "byte": 100,
    "short": 10000,
    "int": 2000000000,
    "long": 200000000000,
    "float": 0.1,
    "date": "2017-01-02T03:04:05",
    "bool": true,
    "ip": "127.0.0.1"
}

Formatting a table for Jupyter Notebook

http://nbviewer.jupyter.org/github/thombashi/pytablewriter/blob/master/examples/ipynb/jupyter_notebook_example.ipynb

Table formatting for Jupyter Notebook

Table formatting for Jupyter Notebook

Write a table using multibyte character

You can use multibyte characters as table data. Multibyte characters also properly padded and aligned.

Sample Code
import pytablewriter

writer = pytablewriter.RstSimpleTableWriter()
writer.table_name = "生成に関するパターン"
writer.header_list = ["パターン名", "概要", "GoF", "Code Complete[1]"]
writer.value_matrix = [
    ["Abstract Factory", "関連する一連のインスタンスを状況に応じて、適切に生成する方法を提供する。", "Yes", "Yes"],
    ["Builder", "複合化されたインスタンスの生成過程を隠蔽する。", "Yes", "No"],
    ["Factory Method", "実際に生成されるインスタンスに依存しない、インスタンスの生成方法を提供する。", "Yes", "Yes"],
    ["Prototype", "同様のインスタンスを生成するために、原型のインスタンスを複製する。", "Yes", "No"],
    ["Singleton", "あるクラスについて、インスタンスが単一であることを保証する。", "Yes", "Yes"],
]
writer.write_table()
Output
Output of multi-byte character tableOutput of multi-byte character table

For more information

More examples are available at http://pytablewriter.rtfd.io/en/latest/pages/examples/index.html

Installation

pip install pytablewriter

Dependencies

Python 2.7+ or 3.4+

Optional Dependencies

Test dependencies

Documentation

http://pytablewriter.rtfd.io/

Related Project

  • pytablereader
    • Tabular data loaded by pytablereader can be written another tabular data format with pytablewriter.