xtable
print console tables. xtable serves as both a class and a command line tool.
Installation
`pip install xtable`
run it through xtableor
python -mxtable`
below is a simple introduction for some basic usage.
use the class
from xtable import xtable
data = [
[1,2,3,4,5],
[11,52,3,4,5],
[11,None,3,4,5],
[11,None,3,None,5],
]
hdr = ['c1','c2','c3','c4','c5']
xt = xtable(data=data,header=hdr)
print(xt)
// result :
c1 c2 c3 c4 c5
--------------
1 2 3 4 5
11 52 3 4 5
11 3 4 5
11 3 5
# test/t2.csv
'''
h1,h2,h3
"asd","sdfsadf",1
"c","cc",233
'''
xt = xtable.init_from_csv("test/t2.csv")
print(xt)
// result :
h1 h2 h3
---------------
asd sdfsadf 1
c cc 233
data = [
{"h1":"v1","h2":"v2","h3":"v3"},
{"h1":"v11","h2":"v22","h3":"v33"},
{"h1":"v11111","h2":"v22222","h3":"v34444"},
]
xt = xtable.init_from_list(data)
print(xt)
// result :
h1 h2 h3
--------------------
v1 v2 v3
v11 v22 v33
v11111 v22222 v34444
// all of them support "xheader".
data = [
{"h1":"v1","h2":"v2","h3":"v3"},
{"h1":"v11","h2":"v22","h3":"v33"},
{"h1":"v11111","h2":"v22222","h3":"v34444"},
]
xt = xtable.init_from_list(data, xheader=["h1","h3"])
print(xt)
xt2 = xtable.init_from_list(data, xheader="h2,h3")
print(xt2)
h1 h3
-------------
v1 v3
v11 v33
v11111 v34444
h2 h3
-------------
v2 v3
v22 v33
v22222 v34444
// output json/yaml/csv/html
data = [
{"h1":"v1","h2":"v2","h3":"v3"},
{"h1":"v11","h2":"v22","h3":"v33"},
{"h1":"v11111","h2":"v22222","h3":"v34444"},
]
xt = xtable.init_from_list(data, xheader=["h1","h3"])
print(xt.csv())
print(xt.yaml())
print(xt.json())
print(xt.html())
// result
- 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>
use the command line
[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
[yonghang@mtp 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@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
pivot
[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
if we look at the source code, we know class xtable support .pivot() as well,
if type(js) is list :
xt = xtable.init_from_json(js,args.header)
if args.pivot :
print(xt.pivot())
else :
print(xt)
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.
[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
xtable take care of lines
[yonghang@W5202860 test]$ cat sql.json | qic
[
{
"name": "sql example 1",
"query": "SELECT QUARTER, REGION, SUM(SALES)\n FROM SALESTABLE\n GROUP BY CUBE (QUARTER, REGION)"
},
{
"name": "sql example 2",
"query": "select name, cast(text as varchar(8000)) \nfrom SYSIBM.SYSVIEWS \n where name='your table name' "
},
{
"name": "sql example 3",
"query": "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"
}
]
[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
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 %