Skip to main content

Format/create and print tables from lists of dicts

Project description

rapidtables

rapidtables is a module for Python 2/3, which does only one thing: converts lists of dictionaries to pre-formatted ascii tables. And it does the job as fast as possible.

rapidtables is focused on speed and is useful for applications which dynamically refresh data in console. The module code is heavily optimized, it uses only tuples inside and on a relatively small tables (<2000 records) it renders them even faster than Pandas.

And unlike other similar modules, rapidtables can output pre-formatted tuples of strings or even tuples of tuples of strings, which allows you to colorize every single column.

Example

from rapidtables import format_table
from termcolor import colored

header, cols = format_table(data, fmt=2) # data is list of dicts
spacer = '  '
print(colored(spacer.join(header), color='blue'))
print(colored('-' * sum([(len(x) + 2) for x in header]), color='grey'))
for c in cols:
    print(colored(c[0], color='white', attrs=['bold']) + spacer, end='')
    print(colored(c[1], color='cyan') + spacer, end='')
    print(colored(c[2], color='yellow'))

colorized table

Pretty cool, isn't it? Actually, it was the most complex example, you can make just

header, rows = format_table(data, fmt=1)

and obtain header + table rows already joined. Or you can use make_table function to return the table out-of-the-box (or print_table to instantly print it), and print it in raw:

print_table(data)
name  salary  job
----  ------  ---------
John    2000  DevOps
Jack    2500  Architect
Ken     1800  Q/A

Quick API reference

format_table

Formats a table. Outputs data in raw, tuple of strings or tuple of tuples of strings, customize headers, separators etc. fmt=0 - raw, 1 - tuple of strings, 2 - tuple of tuples of strings. Read pydoc for more info.

make_table

Generates a ready to output table. Support basic formats:

table = rapidtables.make_table(table, tablefmt='raw')
name  salary  job
-----------------------
John    2000  DevOps
Jack    2500  Architect
Ken     1800  Q/A
table = rapidtables.make_table(table, tablefmt='simple')
name  salary  job
----  ------  ---------
John    2000  DevOps
Jack    2500  Architect
Ken     1800  Q/A
table = rapidtables.make_table(table, tablefmt='md') # Markdown
| name | salary | job       |
|------|--------|-----------|
| John |   2000 | DevOps    |
| Jack |   2500 | Architect |
| Ken  |   1800 | Q/A       |
table = rapidtables.make_table(table, tablefmt='md') # reStructured Text
====  ======  =========
name  salary  job
====  ======  =========
John    2000  DevOps
Jack    2500  Architect
Ken     1800  Q/A
====  ======  =========

print_table

The same as make_table, but prints table to stdout.

Benchmarks

rapidtables is written purely in Python, it will loose to Pandas on a large (3000+ records) tables, but on small it works super fast.

benchmark

Enjoy!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rapidtables-0.0.4.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file rapidtables-0.0.4.tar.gz.

File metadata

  • Download URL: rapidtables-0.0.4.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.3

File hashes

Hashes for rapidtables-0.0.4.tar.gz
Algorithm Hash digest
SHA256 2ed3c80baf878878775c03712a1829d52227ea9b9c5a6bc7f22f2728516eb7a2
MD5 d32fe90787128cc9fca0de63a7c2b048
BLAKE2b-256 7aff42189cea3b06e2b89ac97714ff7b4c7c0a078780505c8fbbc41fca6ffead

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page