print table in console
Project description
xtable
print console tables. xtable serves as both a class and a command line tool.
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)
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.
[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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
xtable-1.0.6.tar.gz
(13.9 kB
view hashes)