Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there's any python library to output dictionary in beautiful ascii table?

Tags:

python

ascii

I have quite huge dictionary in here, which just repeats all over again, and I'm searching for any code to display that dictionary in some ascii table, which would be outputed to command line. At least to have some generated columns for non-dimentional dictionary, and the rest could go as a key:value in cells, something like this:

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

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

it's still better then to see all those u"" {} , in output which is really disturbs when I need that information fast.

like image 516
holms Avatar asked Feb 16 '13 02:02

holms


People also ask

How do I install Python Prettytable?

Download the latest version of PrettyTable from the Downloads tab at this Google code project site. Save the file as "prettytable.py" (not prettytable-x.y.py) in your Python installation's "site-packages" directory.

How do you print a dictionary list in Python?

So we have to get the dictionaries present in the list according to the key. We can get this by using dict. items().

What is Prettytable in Python?

PrettyTable is a Python library that is used to represent tabular data in visually appealing ASCII tables. It is quick and easy to use.


1 Answers

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 module to split the value on multiple lines (example in this gist). 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                        |
+------------------------------+--------------------------------------------------------------+
like image 191
dnozay Avatar answered Oct 21 '22 07:10

dnozay