Is there any python library to output the dictionary into a beautiful ascii table?

I have a rather large dictionary here, which repeats over and over again, and I'm looking for any code to display this dictionary in some ascii table that will be displayed on the command line. At least to create uncoded columns for an implicit dictionary, and the rest may be key: the value in the cells, something like this:

dictionary = {"column1": {"key": "val"}, "column2": "value"}

===================== | column1 | column2 | ===================== | key:val | value | 

it's better to see all those u"" {} , on the output, that really bother me when I need this information quickly.

+4
source share
1 answer

you can use prettytable .

 t = PrettyTable(['key', 'value']) for key, val in dictionary.items(): t.add_row([key, val]) print t 

You can also play with textwrap to split the value into multiple lines ( in this context ). Imagine you want to show the contents of os.environ; results (short version):

 +------------------------------+--------------------------------------------------------------+ | key | value | +------------------------------+--------------------------------------------------------------+ | CFLAGS | -fno-strict-aliasing -O2 -g -pipe -Wall | | | -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector | | | --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE | | | -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall | | | -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector | | | --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE | | | -fPIC -fwrapv | | MAIL | /var/spool/mail/damien | | LS_COLORS | rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=4 | | | 0;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=3 | | | 0;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;3 | | | 1:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01; | | | 31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:* | | | .dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz | | | =01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm= | | | 01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar= | | | 01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=0 | | | 1;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm= | | | 01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm= | | | 01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svg | | | z=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mp | | | eg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m | | | 4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.w | | | mv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.a | | | vi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf | | | =01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv | | | =01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au= | | | 01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp | | | 3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa | | | =01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:*.pdf=00;33:*.ps | | | =00;33:*.ps.gz=00;33:*.txt=00;33:*.patch=00;33:*.diff=00;33: | | | *.log=00;33:*.tex=00;33:*.xls=00;33:*.xlsx=00;33:*.ppt=00;33 | | | :*.pptx=00;33:*.rtf=00;33:*.doc=00;33:*.docx=00;33:*.odt=00; | | | 33:*.ods=00;33:*.odp=00;33:*.xml=00;33:*.epub=00;33:*.abw=00 | | | ;33:*.htm=00;33:*.html=00;33:*.shtml=00;33:*.wpd=00;33: | | GJS_DEBUG_TOPICS | JS ERROR;JS LOG | +------------------------------+--------------------------------------------------------------+ 
+9
source

All Articles