Package: rapidtables
rapidtablesrapidtablesis a module for Python 2/3, which does only one thing: converts
lists of dictionaries to pre-formatted tables. And it does the job as fast as
possible.rapidtablesis focused on speed and is useful for applications which
dynamically refresh data in console. The module code is heavily optimized and
written purely in Python.And unlike other similar modules,rapidtablescan output pre-formatted
generators of strings or even generators of tuples of strings, which allows you
to colorize every single column.InstallpipinstallrapidtablesExample# if you need to keep strict column ordering, use OrderedDict for the rowsdata=[{'name':'John','salary':2000,'job':'DevOps'},{'name':'Jack','salary':2500,'job':'Architect'},{'name':'Diana','salary':None,'job':'Student'},{'name':'Ken','salary':1800,'job':'Q/A'}]fromrapidtablesimportformat_table,FORMAT_GENERATOR_COLSfromtermcolorimportcoloredheader,rows=format_table(data,fmt=FORMAT_GENERATOR_COLS)spacer='  'print(colored(spacer.join(header),color='blue'))print(colored('-'*sum([(len(x)+2)forxinheader]),color='grey'))forrinrows:print(colored(r[0],color='white',attrs=['bold'])+spacer,end='')print(colored(r[1],color='cyan')+spacer,end='')print(colored(r[2],color='yellow'))Pretty cool, isn't it? Actually, it was the most complex example, you can
work with header + table rows already joined:fromrapidtablesimportformat_table,FORMAT_GENERATORheader,rows=format_table(data,fmt=FORMAT_GENERATOR)print(colored(header,color='blue'))print(colored('-'*len(header),color='grey'))forrinrows:print(colored(r,color='yellow'))Or you can usemake_tablefunction to return the table out-of-the-box (orprint_tableto instantly print it), and print it in raw:print_table(data)name  salary  job
----  ------  ---------
John    2000  DevOps
Jack    2500  Architect
Ken     1800  Q/AQuick API referenceformat_tableFormats a table. Outputs data in raw, generator of strings (one string per row)
or generator of tuples of strings (one tuple per row, one string per column):fmt=rapidtables.FORMAT_RAWraw stringfmt=rapidtables.FORMAT_GENERATORgenerator of stringsfmt=rapidtables.FORMAT_GENERATOR_COLSgenerator of tuples of stringsAlign columns:align=rapidtables.ALIGN_LEFTalign all columns to leftalign=rapidtables.ALIGN_NUMBERS_RIGHTalign numbers to right (default)align=rapidtables.ALIGN_RIGHTalign all columns to rightalign=rapidtables.ALIGN_CENTERalign all columns to centeralign=rapidtables.ALIGN_HOMOGENEOUS_NUMBERS_RIGHTalign numbers to right
but consider the table is homogeneous and check col values only to first
number or string (works slightly faster)To predefine aligns, set align to tuple or list:align=(rapidtables.ALIGN_LEFT, rapidtables.ALIGN_RIGHT, ....)number of items in list must match number of columns in table.You may also customize headers, separators etc. Read pydoc for more
info.make_tableGenerates a ready to output table. Supports basic formats:table=rapidtables.make_table(data,tablefmt='raw')name  salary  job
-----------------------
John     2000  DevOps
Jack     2500  Architect
Diana          Student
Ken      1800  Q/Atable=rapidtables.make_table(data,tablefmt='simple')name   salary  job
----   ------  ---------
John     2000  DevOps
Jack     2500  Architect
Diana          Student
Ken      1800  Q/Atable=rapidtables.make_table(data,tablefmt='md')# Markdown| name  | salary | job       |
|-------|--------|-----------|
| John  |   2000 | DevOps    |
| Jack  |   2500 | Architect |
| Diana |        | Student   |
| Ken   |   1800 | Q/A       |table=rapidtables.make_table(data,tablefmt='rst')# reStructured, simple=====  ======  =========
name   salary  job
=====  ======  =========
John     2000  DevOps
Jack     2500  Architect
Diana          Student
Ken      1800  Q/A
=====  ======  =========table=rapidtables.make_table(data,tablefmt='rstgrid')# reStructured, grid+-------+--------+-----------+
| name  | salary | job       |
+=======+========+===========+
| John  |   2000 | DevOps    |
+-------+--------+-----------+
| Jack  |   2500 | Architect |
+-------+--------+-----------+
| Diana |        | Student   |
+-------+--------+-----------+
| Ken   |   1800 | Q/A       |
+-------+--------+-----------+print_tableThe same asmake_table, but prints table to stdout.Benchmarks(Python 3.7)Enjoy!