{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from __future__ import print_function\n", "\n", "from textwrap import dedent\n", "\n", "import pytablewriter\n", "\n", "table_name = \"example_table\"\n", "header_list = [\"int\", \"float\", \"str\", \"bool\", \"mix\", \"time\"]\n", "data = [\n", " [0, 0.1, \"hoge\", True, 0, \"2017-01-01 03:04:05+0900\"],\n", " [2, \"-2.23\", \"foo\", False, None, \"2017-12-23 12:34:51+0900\"],\n", " [3, 0, \"bar\", \"true\", \"inf\", \"2017-03-03 22:44:55+0900\"],\n", " [-10, -9.9, \"\", \"FALSE\", \"nan\", \"2017-01-01 00:00:00+0900\"],\n", "]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "csv\n", "elasticsearch\n", "excel\n", "htm\n", "html\n", "javascript\n", "js\n", "json\n", "json_lines\n", "jsonl\n", "latex_matrix\n", "latex_table\n", "ldjson\n", "ltsv\n", "markdown\n", "md\n", "mediawiki\n", "ndjson\n", "null\n", "numpy\n", "pandas\n", "py\n", "python\n", "rst\n", "rst_csv\n", "rst_csv_table\n", "rst_grid\n", "rst_grid_table\n", "rst_simple\n", "rst_simple_table\n", "space_aligned\n", "sqlite\n", "toml\n", "tsv\n" ] } ], "source": [ "for name in pytablewriter.TableWriterFactory.get_format_name_list():\n", " print(name)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "csv\n", "htm\n", "html\n", "js\n", "json\n", "jsonl\n", "ldjson\n", "ltsv\n", "md\n", "ndjson\n", "py\n", "rst\n", "sqlite\n", "sqlite3\n", "tex\n", "toml\n", "tsv\n", "xls\n", "xlsx\n" ] } ], "source": [ "for name in pytablewriter.TableWriterFactory.get_extension_list():\n", " print(name)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"int\",\"float\",\"str\",\"bool\",\"mix\",\"time\"\n", "0,0.1,\"hoge\",True,0,\"2017-01-01 03:04:05+0900\"\n", "2,-2.23,\"foo\",False,,\"2017-12-23 12:34:51+0900\"\n", "3,0,\"bar\",True,Infinity,\"2017-03-03 22:44:55+0900\"\n", "-10,-9.9,,False,NaN,\"2017-01-01 00:00:00+0900\"\n" ] } ], "source": [ "writer = pytablewriter.CsvTableWriter()\n", "writer.header_list = header_list\n", "writer.value_matrix = data\n", "\n", "writer.write_table()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND \n", "32866 root 20 0 48344 3924 3448 R 5.6 0.2 0:00.03 top \n", " 1 root 20 0 212080 7676 5876 S 0.0 0.4 1:06.56 systemd \n", " 2 root 20 0 0 0 0 S 0.0 0.0 0:01.92 kthreadd \n", " 4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H\n" ] } ], "source": [ "writer = pytablewriter.SpaceAlignedTableWriter()\n", "writer.header_list = [\"PID\", \"USER\", \"PR\", \"NI\", \"VIRT\", \"RES\", \"SHR\", \"S\", \"%CPU\", \"%MEM\", \"TIME+\", \"COMMAND\"]\n", "writer.value_matrix = csv1 = [\n", " [32866, \"root\", 20, 0, 48344, 3924, 3448, \"R\", 5.6, 0.2, \"0:00.03\", \"top\"],\n", " [1, \"root\", 20, 0, 212080, 7676, 5876, \"S\", 0, 0.4, \"1:06.56\", \"systemd\"],\n", " [2, \"root\", 20, 0, 0, 0, 0, \"S\", 0, 0, \"0:01.92\", \"kthreadd\"],\n", " [4, \"root\", 0, -20, 0, 0, 0, \"S\", 0, 0, \"0:00.00\", \"kworker/0:0H\"],\n", "]\n", "\n", "writer.write_table()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"int\"\t\"float\"\t\"str\"\t\"bool\"\t\"mix\"\t\"time\"\n", "0\t0.1\t\"hoge\"\tTrue\t0\t\"2017-01-01 03:04:05+0900\"\n", "2\t-2.23\t\"foo\"\tFalse\t\t\"2017-12-23 12:34:51+0900\"\n", "3\t0\t\"bar\"\tTrue\tInfinity\t\"2017-03-03 22:44:55+0900\"\n", "-10\t-9.9\t\tFalse\tNaN\t\"2017-01-01 00:00:00+0900\"\n" ] } ], "source": [ "writer = pytablewriter.TsvTableWriter()\n", "writer.header_list = header_list\n", "writer.value_matrix = data\n", "\n", "writer.write_table()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "
int | \n", "float | \n", "str | \n", "bool | \n", "mix | \n", "time | \n", "
---|---|---|---|---|---|
0 | \n", "0.10 | \n", "hoge | \n", "True | \n", "0 | \n", "2017-01-01 03:04:05+0900 | \n", "
2 | \n", "-2.23 | \n", "foo | \n", "False | \n", "\n", " | 2017-12-23 12:34:51+0900 | \n", "
3 | \n", "0.00 | \n", "bar | \n", "True | \n", "Infinity | \n", "2017-03-03 22:44:55+0900 | \n", "
-10 | \n", "-9.90 | \n", "\n", " | False | \n", "NaN | \n", "2017-01-01 00:00:00+0900 | \n", "