Archive
Posts Tagged ‘fancy’
fancy text tables
December 28, 2014
Leave a comment
Problem
Instead of simply printing some data on the screen, I wanted to put them in a nicely formatted ASCII table.
Solution
After some research I found a nice package for this purpose: python-tabulate. It supports both Python 2 and Python 3 (yes, from now on it’s also important for me).
Its usage is very simple. Here is a snippet that creates random usernames and passwords:
from tabulate import tabulate
table = []
headers = ["Username #1", "Username #2", "Password #1", "Password #2"]
for _ in range(10):
name1 = get_username_1()
name2 = get_username_2()
pass1 = get_password_1(8)
pass2 = get_password_2(12)
table.append([name1, name2, pass1, pass2])
# print("{:15}{:15}{:15}{:15}".format(name1, name2, pass1, pass2)) # this is the past :)
print(tabulate(table, headers=headers, tablefmt="psql"))
Output:
+---------------+---------------+---------------+---------------+ | Username #1 | Username #2 | Password #1 | Password #2 | |---------------+---------------+---------------+---------------| | Adarah | hasana | ygyQsF6u | uTzPqZMDNJ6x | | Alary | begahi | YqW4aY7q | ipZuX0sX2RFg | | Solita | otomot | Xwliu9yi | IjeFibVFaoZq | | Casony | rikari | fw6dk5gt | zbAXO8gd33Lh | | Anne | asakou | MXsXpz43 | aYNiJTwojULG | | Joby | mgomam | vZjiCuyT | qc3Q9caAenJw | | Kallita | aremon | j1ZD1QU9 | AIEsykmYodfy | | Cara | iumina | 75UzkKgK | lK92GdAxn441 | | Fuscie | goomio | uof2C7ct | HFgVlAZ9PSmv | | Dean | utinon | gycncz9f | 61oJzUGdDVKf | +---------------+---------------+---------------+---------------+
The module supports various formatting styles. For more examples, check out the official page.
Update (20191029)
The project has moved to https://github.com/astanin/python-tabulate .
