## [doc on github.io](https://walkerever.github.io/)
# xtable
print console tables. xtable serves as both a class and a command line tool.
- [installation](#installation)
- [use the class](#use-the-class)
- [command line](#use-the-command-line) from CSV, preformatted table, JSON list to console table, JSON, markdown and more.
- [convert table data to Markdown](#markdown)
----
## Installation
`pip install xtable`
run it through xtable` or `python -mxtable`
----
below is a simple introduction for some basic usage.
## use the class
<pre><font color="#5FD7FF">from</font> xtable <font color="#5FD7FF">import</font> xtable
data = [
[<font color="#C061CB">1</font>,<font color="#C061CB">2</font>,<font color="#C061CB">3</font>,<font color="#C061CB">4</font>,<font color="#C061CB">5</font>],
[<font color="#C061CB">11</font>,<font color="#C061CB">52</font>,<font color="#C061CB">3</font>,<font color="#C061CB">4</font>,<font color="#C061CB">5</font>],
[<font color="#C061CB">11</font>,<font color="#33C7DE"><b>None</b></font>,<font color="#C061CB">3</font>,<font color="#C061CB">4</font>,<font color="#C061CB">5</font>],
[<font color="#C061CB">11</font>,<font color="#33C7DE"><b>None</b></font>,<font color="#C061CB">3</font>,<font color="#33C7DE"><b>None</b></font>,<font color="#C061CB">5</font>],
]
hdr = [<font color="#C061CB">'c1'</font>,<font color="#C061CB">'c2'</font>,<font color="#C061CB">'c3'</font>,<font color="#C061CB">'c4'</font>,<font color="#C061CB">'c5'</font>]
xt = xtable(data=data,header=hdr)
<font color="#33C7DE"><b>print</b></font>(xt)
</pre>
// result :
<pre>c1 c2 c3 c4 c5
--------------
1 2 3 4 5
11 52 3 4 5
11 3 4 5
11 3 5
</pre>
-----
<pre><font color="#33C7DE"># test/t2.csv</font>
<font color="#C061CB">'''</font>
<font color="#C061CB">h1,h2,h3</font>
<font color="#C061CB">"asd","sdfsadf",1</font>
<font color="#C061CB">"c","cc",233</font>
<font color="#C061CB">'''</font>
xt = xtable.init_from_csv(<font color="#C061CB">"test/t2.csv"</font>)
<font color="#33C7DE"><b>print</b></font>(xt)
</pre>
// result :
<pre>h1 h2 h3
---------------
asd sdfsadf 1
c cc 233
</pre>
-----
<pre>
data = [
{<font color="#C061CB">"h1"</font>:<font color="#C061CB">"v1"</font>,<font color="#C061CB">"h2"</font>:<font color="#C061CB">"v2"</font>,<font color="#C061CB">"h3"</font>:<font color="#C061CB">"v3"</font>},
{<font color="#C061CB">"h1"</font>:<font color="#C061CB">"v11"</font>,<font color="#C061CB">"h2"</font>:<font color="#C061CB">"v22"</font>,<font color="#C061CB">"h3"</font>:<font color="#C061CB">"v33"</font>},
{<font color="#C061CB">"h1"</font>:<font color="#C061CB">"v11111"</font>,<font color="#C061CB">"h2"</font>:<font color="#C061CB">"v22222"</font>,<font color="#C061CB">"h3"</font>:<font color="#C061CB">"v34444"</font>},
]
xt = xtable.init_from_list(data)
<font color="#33C7DE"><b>print</b></font>(xt)</pre>
// result :
<pre>
h1 h2 h3
--------------------
v1 v2 v3
v11 v22 v33
v11111 v22222 v34444
</pre>
-----
// all of them support "xheader".
<pre>data = [
{<font color="#C061CB">"h1"</font>:<font color="#C061CB">"v1"</font>,<font color="#C061CB">"h2"</font>:<font color="#C061CB">"v2"</font>,<font color="#C061CB">"h3"</font>:<font color="#C061CB">"v3"</font>},
{<font color="#C061CB">"h1"</font>:<font color="#C061CB">"v11"</font>,<font color="#C061CB">"h2"</font>:<font color="#C061CB">"v22"</font>,<font color="#C061CB">"h3"</font>:<font color="#C061CB">"v33"</font>},
{<font color="#C061CB">"h1"</font>:<font color="#C061CB">"v11111"</font>,<font color="#C061CB">"h2"</font>:<font color="#C061CB">"v22222"</font>,<font color="#C061CB">"h3"</font>:<font color="#C061CB">"v34444"</font>},
]
xt = xtable.init_from_list(data, xheader=[<font color="#C061CB">"h1"</font>,<font color="#C061CB">"h3"</font>])
<font color="#33C7DE"><b>print</b></font>(xt)
xt2 = xtable.init_from_list(data, xheader=<font color="#C061CB">"h2,h3"</font>)
<font color="#33C7DE"><b>print</b></font>(xt2)
</pre>
<pre>h1 h3
-------------
v1 v3
v11 v33
v11111 v34444
h2 h3
-------------
v2 v3
v22 v33
v22222 v34444
</pre>
-----
// output json/yaml/csv/html
<pre>
data = [
{<font color="#C061CB">"h1"</font>:<font color="#C061CB">"v1"</font>,<font color="#C061CB">"h2"</font>:<font color="#C061CB">"v2"</font>,<font color="#C061CB">"h3"</font>:<font color="#C061CB">"v3"</font>},
{<font color="#C061CB">"h1"</font>:<font color="#C061CB">"v11"</font>,<font color="#C061CB">"h2"</font>:<font color="#C061CB">"v22"</font>,<font color="#C061CB">"h3"</font>:<font color="#C061CB">"v33"</font>},
{<font color="#C061CB">"h1"</font>:<font color="#C061CB">"v11111"</font>,<font color="#C061CB">"h2"</font>:<font color="#C061CB">"v22222"</font>,<font color="#C061CB">"h3"</font>:<font color="#C061CB">"v34444"</font>},
]
xt = xtable.init_from_list(data, xheader=[<font color="#C061CB">"h1"</font>,<font color="#C061CB">"h3"</font>])
<font color="#33C7DE"><b>print</b></font>(xt.csv())
<font color="#33C7DE"><b>print</b></font>(xt.yaml())
<font color="#33C7DE"><b>print</b></font>(xt.json())
<font color="#33C7DE"><b>print</b></font>(xt.html())
</pre>
// result
<pre>- h1: v1
h3: v3
- h1: v11
h3: v33
- h1: v11111
h3: v34444
[
{
"h1": "v1",
"h3": "v3"
},
{
"h1": "v11",
"h3": "v33"
},
{
"h1": "v11111",
"h3": "v34444"
}
]
<table border=1 style="border-collapse:collapse;">
<tr>
<td><b>h1</b></td>
<td><b>h3</b></td>
</tr>
<tr>
<td>v1</td>
<td>v3</td>
</tr>
<tr>
<td>v11</td>
<td>v33</td>
</tr>
<tr>
<td>v11111</td>
<td>v34444</td>
</tr>
</pre>
-----
## use the command line
<pre>[yonghang@mtp xtable]$ cat test/t1.txt
a b c
121 1212 12
12 332 2323
[yonghang@mtp xtable]$ cat test/t1.txt | xtable
a b c
-------------
121 1212 12
12 332 2323
[yonghang@mtp xtable]$
[yonghang@mtp xtable]$ cat test/t2.csv
h1,h2,h3
"asd","sdfsadf",1
"c","cc",233
[yonghang@mtp xtable]$
[yonghang@mtp xtable]$ cat test/t2.csv | xtable -b","
h1 h2 h3
-------------------
"asd" "sdfsadf" 1
"c" "cc" 233</pre>
-----
<pre>[yonghang@mtp xtable]$ cat test/t3.json | qic
[
{
<font color="#008700"><b>"userId"</b></font>: <font color="#626262">1</font>,
<font color="#008700"><b>"firstName"</b></font>: <font color="#AF0000">"Krish"</font>,
<font color="#008700"><b>"lastName"</b></font>: <font color="#AF0000">"Lee"</font>,
<font color="#008700"><b>"phoneNumber"</b></font>: <font color="#AF0000">"123456"</font>,
<font color="#008700"><b>"emailAddress"</b></font>: <font color="#AF0000">"krish.lee@learningcontainer.com"</font>
},
{
<font color="#008700"><b>"userId"</b></font>: <font color="#626262">2</font>,
<font color="#008700"><b>"firstName"</b></font>: <font color="#AF0000">"racks"</font>,
<font color="#008700"><b>"lastName"</b></font>: <font color="#AF0000">"jacson"</font>,
<font color="#008700"><b>"phoneNumber"</b></font>: <font color="#AF0000">"123456"</font>,
<font color="#008700"><b>"emailAddress"</b></font>: <font color="#AF0000">"racks.jacson@learningcontainer.com"</font>
},
{
<font color="#008700"><b>"userId"</b></font>: <font color="#626262">3</font>,
<font color="#008700"><b>"firstName"</b></font>: <font color="#AF0000">"denial"</font>,
<font color="#008700"><b>"lastName"</b></font>: <font color="#AF0000">"roast"</font>,
<font color="#008700"><b>"phoneNumber"</b></font>: <font color="#AF0000">"33333333"</font>,
<font color="#008700"><b>"emailAddress"</b></font>: <font color="#AF0000">"denial.roast@learningcontainer.com"</font>
},
{
<font color="#008700"><b>"userId"</b></font>: <font color="#626262">4</font>,
<font color="#008700"><b>"firstName"</b></font>: <font color="#AF0000">"devid"</font>,
<font color="#008700"><b>"lastName"</b></font>: <font color="#AF0000">"neo"</font>,
<font color="#008700"><b>"phoneNumber"</b></font>: <font color="#AF0000">"222222222"</font>,
<font color="#008700"><b>"emailAddress"</b></font>: <font color="#AF0000">"devid.neo@learningcontainer.com"</font>
},
{
<font color="#008700"><b>"userId"</b></font>: <font color="#626262">5</font>,
<font color="#008700"><b>"firstName"</b></font>: <font color="#AF0000">"jone"</font>,
<font color="#008700"><b>"lastName"</b></font>: <font color="#AF0000">"mac"</font>,
<font color="#008700"><b>"phoneNumber"</b></font>: <font color="#AF0000">"111111111"</font>,
<font color="#008700"><b>"emailAddress"</b></font>: <font color="#AF0000">"jone.mac@learningcontainer.com"</font>
}
]
[yonghang@mtp xtable]$ cat test/t3.json | xtable
userId firstName lastName phoneNumber emailAddress
------------------------------------------------------------------------
1 Krish Lee 123456 krish.lee@learningcontainer.com
2 racks jacson 123456 racks.jacson@learningcontainer.com
3 denial roast 33333333 denial.roast@learningcontainer.com
4 devid neo 222222222 devid.neo@learningcontainer.com
5 jone mac 111111111 jone.mac@learningcontainer.com
</pre>
-----
### pivot
<pre>[yonghang@mtp xtable]$ cat test/t3.json | xtable -v
userId : 1
firstName : Krish
lastName : Lee
phoneNumber : 123456
emailAddress : krish.lee@learningcontainer.com
--
userId : 2
firstName : racks
lastName : jacson
phoneNumber : 123456
emailAddress : racks.jacson@learningcontainer.com
--
userId : 3
firstName : denial
lastName : roast
phoneNumber : 33333333
emailAddress : denial.roast@learningcontainer.com
--
userId : 4
firstName : devid
lastName : neo
phoneNumber : 222222222
emailAddress : devid.neo@learningcontainer.com
--
userId : 5
firstName : jone
lastName : mac
phoneNumber : 111111111
emailAddress : jone.mac@learningcontainer.com
</pre>
if we look at the source code, we know class xtable support .pivot() as well,
<pre> <font color="#E9AD0C">if</font> <font color="#33C7DE"><b>type</b></font>(js) <font color="#E9AD0C">is</font> <font color="#33C7DE"><b>list</b></font> :
xt = xtable.init_from_json(js,args.header)
<font color="#E9AD0C">if</font> args.pivot :
<font color="#33C7DE"><b>print</b></font>(xt.pivot<span style="background-color:#2AA1B3">()</span>)
<font color="#E9AD0C">else</font> :
<font color="#33C7DE"><b>print</b></font>(xt)
</pre>
-----
### when header has space...
some command output is already a table, only not that decent, eg. as below. xtable will help a little bit. `-c` told xtable that `container id` is together while `-t` told xtable the input stream is already in "table" format.
<pre>[yonghang@mtp xtable]$ sudo podman ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eeb5db3c4f9a docker.io/library/nginx:latest nginx -g daemon o... About a minute ago Exited (0) About a minute ago romantic_hamilton
5ef267563b44 docker.io/library/httpd:latest httpd-foreground 55 seconds ago Exited (0) 6 seconds ago sad_lamarr
[yonghang@mtp xtable]$
[yonghang@mtp xtable]$
[yonghang@mtp xtable]$ sudo podman ps --all | xtable -c "CONTAINER ID" -t
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
----------------------------------------------------------------------------------------------------------------------------------------
eeb5db3c4f9a docker.io/library/nginx:latest nginx -g daemon o... About a minute ago Exited (0) About a minute ago romantic_hamilto
5ef267563b44 docker.io/library/httpd:latest httpd-foreground About a minute ago Exited (0) 28 seconds ago sad_lamar
[yonghang@mtp xtable]$
[yonghang@mtp xtable]$ sudo podman ps --all | xtable -c "CONTAINER ID" -tv
CONTAINER ID : eeb5db3c4f9a
IMAGE : docker.io/library/nginx:latest
COMMAND : nginx -g daemon o...
CREATED : About a minute ago
STATUS : Exited (0) About a minute ago
PORTS :
NAMES : romantic_hamilto
--
CONTAINER ID : 5ef267563b44
IMAGE : docker.io/library/httpd:latest
COMMAND : httpd-foreground
CREATED : About a minute ago
STATUS : Exited (0) 32 seconds ago
PORTS :
NAMES : sad_lamar
</pre>
### xtable take care of lines
<pre>[yonghang@W5202860 test]$ cat sql.json | qic
[
{
<font color="#008700"><b>"name"</b></font>: <font color="#AF0000">"sql example 1"</font>,
<font color="#008700"><b>"query"</b></font>: <font color="#AF0000">"SELECT QUARTER, REGION, SUM(SALES)\n FROM SALESTABLE\n GROUP BY CUBE (QUARTER, REGION)"</font>
},
{
<font color="#008700"><b>"name"</b></font>: <font color="#AF0000">"sql example 2"</font>,
<font color="#008700"><b>"query"</b></font>: <font color="#AF0000">"select name, cast(text as varchar(8000)) \nfrom SYSIBM.SYSVIEWS \n where name='your table name' "</font>
},
{
<font color="#008700"><b>"name"</b></font>: <font color="#AF0000">"sql example 3"</font>,
<font color="#008700"><b>"query"</b></font>: <font color="#AF0000">"select Id, max(V1),max(V2),max(V3) from \n (\n select ID,Value V1,'' V2,'' V3 from A where Code=1 \n union all \n select ID,'' V1, Value V2,'' V3 from A where Code=2 \n union all \n select ID,'' V1, '' V2,Value V3 from A where Code=3 \n) AG\n group by ID"</font>
}
]
[yonghang@W5202860 test]$
[yonghang@W5202860 test]$ cat sql.json | xtable
name query
-------------------------------------------------------------------
sql example 1 SELECT QUARTER, REGION, SUM(SALES)
FROM SALESTABLE
GROUP BY CUBE (QUARTER, REGION)
sql example 2 select name, cast(text as varchar(8000))
from SYSIBM.SYSVIEWS
where name='your table name'
sql example 3 select Id, max(V1),max(V2),max(V3) from
(
select ID,Value V1,'' V2,'' V3 from A where Code=1
union all
select ID,'' V1, Value V2,'' V3 from A where Code=2
union all
select ID,'' V1, '' V2,Value V3 from A where Code=3
) AG
group by ID
</pre>
## markdown
```
yonghang@air xtable % cat test/t2.csv
h1,h2,h3
"asd","sdfsadf",1
"c","cc",233
yonghang@air xtable % cat test/t2.csv | xtable -F md
| h1,h2,h3 |
| ----------------- |
| "asd","sdfsadf",1 |
| "c","cc",233 |
yonghang@air xtable %
yonghang@air xtable % cat test/t1.txt
a b c
121 1212 12
12 332 2323
yonghang@air xtable % cat test/t1.txt | xtable -F md
| a | b | c |
| ---- | ---- | ---- |
| 121 | 1212 | 12 |
| 12 | 332 | 2323 |
yonghang@air xtable %
yonghang@air xtable % cat test/t3.json | qic
[
{
"userId": 1,
"firstName": "Krish",
"lastName": "Lee",
"phoneNumber": "123456",
"emailAddress": "krish.lee@learningcontainer.com"
},
{
"userId": 2,
"firstName": "racks",
"lastName": "jacson",
"phoneNumber": "123456",
"emailAddress": "racks.jacson@learningcontainer.com"
},
{
"userId": 3,
"firstName": "denial",
"lastName": "roast",
"phoneNumber": "33333333",
"emailAddress": "denial.roast@learningcontainer.com"
},
{
"userId": 4,
"firstName": "devid",
"lastName": "neo",
"phoneNumber": "222222222",
"emailAddress": "devid.neo@learningcontainer.com"
},
{
"userId": 5,
"firstName": "jone",
"lastName": "mac",
"phoneNumber": "111111111",
"emailAddress": "jone.mac@learningcontainer.com"
}
]
yonghang@air xtable %
yonghang@air xtable % cat test/t3.json | xtable -F md
| userId | firstName | lastName | phoneNumber | emailAddress |
| ------ | --------- | -------- | ----------- | ---------------------------------- |
| 1 | Krish | Lee | 123456 | krish.lee@learningcontainer.com |
| 2 | racks | jacson | 123456 | racks.jacson@learningcontainer.com |
| 3 | denial | roast | 33333333 | denial.roast@learningcontainer.com |
| 4 | devid | neo | 222222222 | devid.neo@learningcontainer.com |
| 5 | jone | mac | 111111111 | jone.mac@learningcontainer.com |
yonghang@air xtable %
```
Raw data
{
"_id": null,
"home_page": "https://github.com/walkerever/xtable",
"name": "xtable",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "xtable,table,simpletable,quicktable",
"author": "Yonghang Wang",
"author_email": "wyhang@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/85/57/904d5bedd62e86498b2313b7003aba7284727f2f2882dc64d94a3ea3e7ad/xtable-1.1.6.tar.gz",
"platform": null,
"description": "## [doc on github.io](https://walkerever.github.io/)\n\n# xtable\n\nprint console tables. xtable serves as both a class and a command line tool.\n\n- [installation](#installation)\n- [use the class](#use-the-class)\n- [command line](#use-the-command-line) from CSV, preformatted table, JSON list to console table, JSON, markdown and more.\n- [convert table data to Markdown](#markdown)\n\n----\n\n\n## Installation\n `pip install xtable`\n run it through xtable` or `python -mxtable`\n\n----\n\nbelow is a simple introduction for some basic usage.\n\n## use the class\n<pre><font color=\"#5FD7FF\">from</font> xtable <font color=\"#5FD7FF\">import</font> xtable\n\ndata = [\n [<font color=\"#C061CB\">1</font>,<font color=\"#C061CB\">2</font>,<font color=\"#C061CB\">3</font>,<font color=\"#C061CB\">4</font>,<font color=\"#C061CB\">5</font>],\n [<font color=\"#C061CB\">11</font>,<font color=\"#C061CB\">52</font>,<font color=\"#C061CB\">3</font>,<font color=\"#C061CB\">4</font>,<font color=\"#C061CB\">5</font>],\n [<font color=\"#C061CB\">11</font>,<font color=\"#33C7DE\"><b>None</b></font>,<font color=\"#C061CB\">3</font>,<font color=\"#C061CB\">4</font>,<font color=\"#C061CB\">5</font>],\n [<font color=\"#C061CB\">11</font>,<font color=\"#33C7DE\"><b>None</b></font>,<font color=\"#C061CB\">3</font>,<font color=\"#33C7DE\"><b>None</b></font>,<font color=\"#C061CB\">5</font>],\n ]\nhdr = [<font color=\"#C061CB\">'c1'</font>,<font color=\"#C061CB\">'c2'</font>,<font color=\"#C061CB\">'c3'</font>,<font color=\"#C061CB\">'c4'</font>,<font color=\"#C061CB\">'c5'</font>]\n\nxt = xtable(data=data,header=hdr)\n<font color=\"#33C7DE\"><b>print</b></font>(xt)\n\n</pre>\n\n// result : \n<pre>c1 c2 c3 c4 c5\n--------------\n1 2 3 4 5\n11 52 3 4 5\n11 3 4 5\n11 3 5\n</pre>\n-----\n\n<pre><font color=\"#33C7DE\"># test/t2.csv</font>\n<font color=\"#C061CB\">'''</font>\n<font color=\"#C061CB\">h1,h2,h3</font>\n<font color=\"#C061CB\">"asd","sdfsadf",1</font>\n<font color=\"#C061CB\">"c","cc",233</font>\n<font color=\"#C061CB\">'''</font>\n\nxt = xtable.init_from_csv(<font color=\"#C061CB\">"test/t2.csv"</font>)\n<font color=\"#33C7DE\"><b>print</b></font>(xt)\n</pre>\n\n// result :\n<pre>h1 h2 h3\n---------------\nasd sdfsadf 1\nc cc 233\n</pre>\n\n-----\n<pre>\ndata = [\n {<font color=\"#C061CB\">"h1"</font>:<font color=\"#C061CB\">"v1"</font>,<font color=\"#C061CB\">"h2"</font>:<font color=\"#C061CB\">"v2"</font>,<font color=\"#C061CB\">"h3"</font>:<font color=\"#C061CB\">"v3"</font>},\n {<font color=\"#C061CB\">"h1"</font>:<font color=\"#C061CB\">"v11"</font>,<font color=\"#C061CB\">"h2"</font>:<font color=\"#C061CB\">"v22"</font>,<font color=\"#C061CB\">"h3"</font>:<font color=\"#C061CB\">"v33"</font>},\n {<font color=\"#C061CB\">"h1"</font>:<font color=\"#C061CB\">"v11111"</font>,<font color=\"#C061CB\">"h2"</font>:<font color=\"#C061CB\">"v22222"</font>,<font color=\"#C061CB\">"h3"</font>:<font color=\"#C061CB\">"v34444"</font>},\n ]\nxt = xtable.init_from_list(data)\n<font color=\"#33C7DE\"><b>print</b></font>(xt)</pre>\n\n// result :\n<pre>\nh1 h2 h3\n--------------------\nv1 v2 v3\nv11 v22 v33\nv11111 v22222 v34444\n\n</pre>\n\n-----\n\n// all of them support \"xheader\".\n\n<pre>data = [\n {<font color=\"#C061CB\">"h1"</font>:<font color=\"#C061CB\">"v1"</font>,<font color=\"#C061CB\">"h2"</font>:<font color=\"#C061CB\">"v2"</font>,<font color=\"#C061CB\">"h3"</font>:<font color=\"#C061CB\">"v3"</font>},\n {<font color=\"#C061CB\">"h1"</font>:<font color=\"#C061CB\">"v11"</font>,<font color=\"#C061CB\">"h2"</font>:<font color=\"#C061CB\">"v22"</font>,<font color=\"#C061CB\">"h3"</font>:<font color=\"#C061CB\">"v33"</font>},\n {<font color=\"#C061CB\">"h1"</font>:<font color=\"#C061CB\">"v11111"</font>,<font color=\"#C061CB\">"h2"</font>:<font color=\"#C061CB\">"v22222"</font>,<font color=\"#C061CB\">"h3"</font>:<font color=\"#C061CB\">"v34444"</font>},\n ]\nxt = xtable.init_from_list(data, xheader=[<font color=\"#C061CB\">"h1"</font>,<font color=\"#C061CB\">"h3"</font>])\n<font color=\"#33C7DE\"><b>print</b></font>(xt)\n\nxt2 = xtable.init_from_list(data, xheader=<font color=\"#C061CB\">"h2,h3"</font>)\n<font color=\"#33C7DE\"><b>print</b></font>(xt2)\n\n</pre>\n\n<pre>h1 h3\n-------------\nv1 v3\nv11 v33\nv11111 v34444\n\nh2 h3\n-------------\nv2 v3\nv22 v33\nv22222 v34444\n</pre>\n\n-----\n\n// output json/yaml/csv/html\n\n<pre>\n\ndata = [\n {<font color=\"#C061CB\">"h1"</font>:<font color=\"#C061CB\">"v1"</font>,<font color=\"#C061CB\">"h2"</font>:<font color=\"#C061CB\">"v2"</font>,<font color=\"#C061CB\">"h3"</font>:<font color=\"#C061CB\">"v3"</font>},\n {<font color=\"#C061CB\">"h1"</font>:<font color=\"#C061CB\">"v11"</font>,<font color=\"#C061CB\">"h2"</font>:<font color=\"#C061CB\">"v22"</font>,<font color=\"#C061CB\">"h3"</font>:<font color=\"#C061CB\">"v33"</font>},\n {<font color=\"#C061CB\">"h1"</font>:<font color=\"#C061CB\">"v11111"</font>,<font color=\"#C061CB\">"h2"</font>:<font color=\"#C061CB\">"v22222"</font>,<font color=\"#C061CB\">"h3"</font>:<font color=\"#C061CB\">"v34444"</font>},\n ]\nxt = xtable.init_from_list(data, xheader=[<font color=\"#C061CB\">"h1"</font>,<font color=\"#C061CB\">"h3"</font>])\n<font color=\"#33C7DE\"><b>print</b></font>(xt.csv())\n<font color=\"#33C7DE\"><b>print</b></font>(xt.yaml())\n<font color=\"#33C7DE\"><b>print</b></font>(xt.json())\n<font color=\"#33C7DE\"><b>print</b></font>(xt.html())\n\n</pre>\n\n// result\n<pre>- h1: v1\n h3: v3\n- h1: v11\n h3: v33\n- h1: v11111\n h3: v34444\n\n[\n {\n "h1": "v1",\n "h3": "v3"\n },\n {\n "h1": "v11",\n "h3": "v33"\n },\n {\n "h1": "v11111",\n "h3": "v34444"\n }\n]\n<table border=1 style="border-collapse:collapse;">\n<tr>\n<td><b>h1</b></td>\n<td><b>h3</b></td>\n</tr>\n<tr>\n<td>v1</td>\n<td>v3</td>\n</tr>\n<tr>\n<td>v11</td>\n<td>v33</td>\n</tr>\n<tr>\n<td>v11111</td>\n<td>v34444</td>\n</tr>\n</pre>\n\n-----\n\n## use the command line\n\n<pre>[yonghang@mtp xtable]$ cat test/t1.txt \na b c\n121 1212 12\n12 332 2323\n[yonghang@mtp xtable]$ cat test/t1.txt | xtable \na b c\n-------------\n121 1212 12\n12 332 2323\n[yonghang@mtp xtable]$ \n[yonghang@mtp xtable]$ cat test/t2.csv \nh1,h2,h3\n"asd","sdfsadf",1\n"c","cc",233\n[yonghang@mtp xtable]$ \n[yonghang@mtp xtable]$ cat test/t2.csv | xtable -b","\nh1 h2 h3\n-------------------\n"asd" "sdfsadf" 1\n"c" "cc" 233</pre>\n\n-----\n\n<pre>[yonghang@mtp xtable]$ cat test/t3.json | qic\n[\n {\n <font color=\"#008700\"><b>"userId"</b></font>: <font color=\"#626262\">1</font>,\n <font color=\"#008700\"><b>"firstName"</b></font>: <font color=\"#AF0000\">"Krish"</font>,\n <font color=\"#008700\"><b>"lastName"</b></font>: <font color=\"#AF0000\">"Lee"</font>,\n <font color=\"#008700\"><b>"phoneNumber"</b></font>: <font color=\"#AF0000\">"123456"</font>,\n <font color=\"#008700\"><b>"emailAddress"</b></font>: <font color=\"#AF0000\">"krish.lee@learningcontainer.com"</font>\n },\n {\n <font color=\"#008700\"><b>"userId"</b></font>: <font color=\"#626262\">2</font>,\n <font color=\"#008700\"><b>"firstName"</b></font>: <font color=\"#AF0000\">"racks"</font>,\n <font color=\"#008700\"><b>"lastName"</b></font>: <font color=\"#AF0000\">"jacson"</font>,\n <font color=\"#008700\"><b>"phoneNumber"</b></font>: <font color=\"#AF0000\">"123456"</font>,\n <font color=\"#008700\"><b>"emailAddress"</b></font>: <font color=\"#AF0000\">"racks.jacson@learningcontainer.com"</font>\n },\n {\n <font color=\"#008700\"><b>"userId"</b></font>: <font color=\"#626262\">3</font>,\n <font color=\"#008700\"><b>"firstName"</b></font>: <font color=\"#AF0000\">"denial"</font>,\n <font color=\"#008700\"><b>"lastName"</b></font>: <font color=\"#AF0000\">"roast"</font>,\n <font color=\"#008700\"><b>"phoneNumber"</b></font>: <font color=\"#AF0000\">"33333333"</font>,\n <font color=\"#008700\"><b>"emailAddress"</b></font>: <font color=\"#AF0000\">"denial.roast@learningcontainer.com"</font>\n },\n {\n <font color=\"#008700\"><b>"userId"</b></font>: <font color=\"#626262\">4</font>,\n <font color=\"#008700\"><b>"firstName"</b></font>: <font color=\"#AF0000\">"devid"</font>,\n <font color=\"#008700\"><b>"lastName"</b></font>: <font color=\"#AF0000\">"neo"</font>,\n <font color=\"#008700\"><b>"phoneNumber"</b></font>: <font color=\"#AF0000\">"222222222"</font>,\n <font color=\"#008700\"><b>"emailAddress"</b></font>: <font color=\"#AF0000\">"devid.neo@learningcontainer.com"</font>\n },\n {\n <font color=\"#008700\"><b>"userId"</b></font>: <font color=\"#626262\">5</font>,\n <font color=\"#008700\"><b>"firstName"</b></font>: <font color=\"#AF0000\">"jone"</font>,\n <font color=\"#008700\"><b>"lastName"</b></font>: <font color=\"#AF0000\">"mac"</font>,\n <font color=\"#008700\"><b>"phoneNumber"</b></font>: <font color=\"#AF0000\">"111111111"</font>,\n <font color=\"#008700\"><b>"emailAddress"</b></font>: <font color=\"#AF0000\">"jone.mac@learningcontainer.com"</font>\n }\n]\n\n[yonghang@mtp xtable]$ cat test/t3.json | xtable\nuserId firstName lastName phoneNumber emailAddress\n------------------------------------------------------------------------\n1 Krish Lee 123456 krish.lee@learningcontainer.com\n2 racks jacson 123456 racks.jacson@learningcontainer.com\n3 denial roast 33333333 denial.roast@learningcontainer.com\n4 devid neo 222222222 devid.neo@learningcontainer.com\n5 jone mac 111111111 jone.mac@learningcontainer.com\n</pre>\n\n-----\n### pivot \n<pre>[yonghang@mtp xtable]$ cat test/t3.json | xtable -v\nuserId : 1\nfirstName : Krish\nlastName : Lee\nphoneNumber : 123456\nemailAddress : krish.lee@learningcontainer.com\n--\nuserId : 2\nfirstName : racks\nlastName : jacson\nphoneNumber : 123456\nemailAddress : racks.jacson@learningcontainer.com\n--\nuserId : 3\nfirstName : denial\nlastName : roast\nphoneNumber : 33333333\nemailAddress : denial.roast@learningcontainer.com\n--\nuserId : 4\nfirstName : devid\nlastName : neo\nphoneNumber : 222222222\nemailAddress : devid.neo@learningcontainer.com\n--\nuserId : 5\nfirstName : jone\nlastName : mac\nphoneNumber : 111111111\nemailAddress : jone.mac@learningcontainer.com\n</pre>\n\nif we look at the source code, we know class xtable support .pivot() as well,\n\n<pre> <font color=\"#E9AD0C\">if</font> <font color=\"#33C7DE\"><b>type</b></font>(js) <font color=\"#E9AD0C\">is</font> <font color=\"#33C7DE\"><b>list</b></font> :\n xt = xtable.init_from_json(js,args.header)\n <font color=\"#E9AD0C\">if</font> args.pivot :\n <font color=\"#33C7DE\"><b>print</b></font>(xt.pivot<span style=\"background-color:#2AA1B3\">()</span>)\n <font color=\"#E9AD0C\">else</font> :\n <font color=\"#33C7DE\"><b>print</b></font>(xt)\n</pre>\n\n\n-----\n### when header has space...\nsome command output is already a table, only not that decent, eg. as below. xtable will help a little bit. `-c` told xtable that `container id` is together while `-t` told xtable the input stream is already in \"table\" format.\n\n<pre>[yonghang@mtp xtable]$ sudo podman ps --all\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\neeb5db3c4f9a docker.io/library/nginx:latest nginx -g daemon o... About a minute ago Exited (0) About a minute ago romantic_hamilton\n5ef267563b44 docker.io/library/httpd:latest httpd-foreground 55 seconds ago Exited (0) 6 seconds ago sad_lamarr\n[yonghang@mtp xtable]$ \n[yonghang@mtp xtable]$ \n[yonghang@mtp xtable]$ sudo podman ps --all | xtable -c "CONTAINER ID" -t\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\n----------------------------------------------------------------------------------------------------------------------------------------\neeb5db3c4f9a docker.io/library/nginx:latest nginx -g daemon o... About a minute ago Exited (0) About a minute ago romantic_hamilto\n5ef267563b44 docker.io/library/httpd:latest httpd-foreground About a minute ago Exited (0) 28 seconds ago sad_lamar\n[yonghang@mtp xtable]$ \n[yonghang@mtp xtable]$ sudo podman ps --all | xtable -c "CONTAINER ID" -tv\nCONTAINER ID : eeb5db3c4f9a\nIMAGE : docker.io/library/nginx:latest\nCOMMAND : nginx -g daemon o...\nCREATED : About a minute ago\nSTATUS : Exited (0) About a minute ago\nPORTS : \nNAMES : romantic_hamilto\n--\nCONTAINER ID : 5ef267563b44\nIMAGE : docker.io/library/httpd:latest\nCOMMAND : httpd-foreground\nCREATED : About a minute ago\nSTATUS : Exited (0) 32 seconds ago\nPORTS : \nNAMES : sad_lamar\n</pre>\n\n### xtable take care of lines\n\n<pre>[yonghang@W5202860 test]$ cat sql.json | qic\n[\n {\n <font color=\"#008700\"><b>"name"</b></font>: <font color=\"#AF0000\">"sql example 1"</font>,\n <font color=\"#008700\"><b>"query"</b></font>: <font color=\"#AF0000\">"SELECT QUARTER, REGION, SUM(SALES)\\n FROM SALESTABLE\\n GROUP BY CUBE (QUARTER, REGION)"</font>\n },\n {\n <font color=\"#008700\"><b>"name"</b></font>: <font color=\"#AF0000\">"sql example 2"</font>,\n <font color=\"#008700\"><b>"query"</b></font>: <font color=\"#AF0000\">"select name, cast(text as varchar(8000)) \\nfrom SYSIBM.SYSVIEWS \\n where name='your table name' "</font>\n },\n {\n <font color=\"#008700\"><b>"name"</b></font>: <font color=\"#AF0000\">"sql example 3"</font>,\n <font color=\"#008700\"><b>"query"</b></font>: <font color=\"#AF0000\">"select Id, max(V1),max(V2),max(V3) from \\n (\\n select ID,Value V1,'' V2,'' V3 from A where Code=1 \\n union all \\n select ID,'' V1, Value V2,'' V3 from A where Code=2 \\n union all \\n select ID,'' V1, '' V2,Value V3 from A where Code=3 \\n) AG\\n group by ID"</font>\n }\n]\n\n[yonghang@W5202860 test]$ \n[yonghang@W5202860 test]$ cat sql.json | xtable\nname query\n-------------------------------------------------------------------\nsql example 1 SELECT QUARTER, REGION, SUM(SALES)\n FROM SALESTABLE\n GROUP BY CUBE (QUARTER, REGION)\nsql example 2 select name, cast(text as varchar(8000))\n from SYSIBM.SYSVIEWS\n where name='your table name'\nsql example 3 select Id, max(V1),max(V2),max(V3) from\n (\n select ID,Value V1,'' V2,'' V3 from A where Code=1\n union all\n select ID,'' V1, Value V2,'' V3 from A where Code=2\n union all\n select ID,'' V1, '' V2,Value V3 from A where Code=3\n ) AG\n group by ID\n</pre>\n\n\n## markdown\n\n```\nyonghang@air xtable % cat test/t2.csv\nh1,h2,h3\n\"asd\",\"sdfsadf\",1\n\"c\",\"cc\",233\n\nyonghang@air xtable % cat test/t2.csv | xtable -F md\n| h1,h2,h3 |\n| ----------------- |\n| \"asd\",\"sdfsadf\",1 |\n| \"c\",\"cc\",233 |\nyonghang@air xtable %\nyonghang@air xtable % cat test/t1.txt\na b c\n121 1212 12\n12 332 2323\nyonghang@air xtable % cat test/t1.txt | xtable -F md\n| a | b | c |\n| ---- | ---- | ---- |\n| 121 | 1212 | 12 |\n| 12 | 332 | 2323 |\nyonghang@air xtable %\nyonghang@air xtable % cat test/t3.json | qic\n[\n {\n \"userId\": 1,\n \"firstName\": \"Krish\",\n \"lastName\": \"Lee\",\n \"phoneNumber\": \"123456\",\n \"emailAddress\": \"krish.lee@learningcontainer.com\"\n },\n {\n \"userId\": 2,\n \"firstName\": \"racks\",\n \"lastName\": \"jacson\",\n \"phoneNumber\": \"123456\",\n \"emailAddress\": \"racks.jacson@learningcontainer.com\"\n },\n {\n \"userId\": 3,\n \"firstName\": \"denial\",\n \"lastName\": \"roast\",\n \"phoneNumber\": \"33333333\",\n \"emailAddress\": \"denial.roast@learningcontainer.com\"\n },\n {\n \"userId\": 4,\n \"firstName\": \"devid\",\n \"lastName\": \"neo\",\n \"phoneNumber\": \"222222222\",\n \"emailAddress\": \"devid.neo@learningcontainer.com\"\n },\n {\n \"userId\": 5,\n \"firstName\": \"jone\",\n \"lastName\": \"mac\",\n \"phoneNumber\": \"111111111\",\n \"emailAddress\": \"jone.mac@learningcontainer.com\"\n }\n]\n\nyonghang@air xtable %\nyonghang@air xtable % cat test/t3.json | xtable -F md\n| userId | firstName | lastName | phoneNumber | emailAddress |\n| ------ | --------- | -------- | ----------- | ---------------------------------- |\n| 1 | Krish | Lee | 123456 | krish.lee@learningcontainer.com |\n| 2 | racks | jacson | 123456 | racks.jacson@learningcontainer.com |\n| 3 | denial | roast | 33333333 | denial.roast@learningcontainer.com |\n| 4 | devid | neo | 222222222 | devid.neo@learningcontainer.com |\n| 5 | jone | mac | 111111111 | jone.mac@learningcontainer.com |\nyonghang@air xtable %\n```\n",
"bugtrack_url": null,
"license": "Apache 2",
"summary": "operate table format in console. console table, csv, json, yaml and markdown.",
"version": "1.1.6",
"split_keywords": [
"xtable",
"table",
"simpletable",
"quicktable"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8557904d5bedd62e86498b2313b7003aba7284727f2f2882dc64d94a3ea3e7ad",
"md5": "6d6e2958324091e1d13d7b6a9341ff75",
"sha256": "0c017be27bb6e2e1d8934cecb7e8da868d8c117ca0d44e96c2f0c1deff7bfbb7"
},
"downloads": -1,
"filename": "xtable-1.1.6.tar.gz",
"has_sig": false,
"md5_digest": "6d6e2958324091e1d13d7b6a9341ff75",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 18901,
"upload_time": "2023-01-05T15:39:42",
"upload_time_iso_8601": "2023-01-05T15:39:42.972215Z",
"url": "https://files.pythonhosted.org/packages/85/57/904d5bedd62e86498b2313b7003aba7284727f2f2882dc64d94a3ea3e7ad/xtable-1.1.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-05 15:39:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "walkerever",
"github_project": "xtable",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "xtable"
}