Using python2.7, I'm trying to print to screen tabular data.
This is roughly what my code looks like:
for i in mylist: print "{}\t|{}\t|".format (i, f(i))
The problem is that, depending on the length of i
or f(i)
the data won't be aligned.
This is what I'm getting:
|foo |bar | |foobo |foobar |
What I want to get:
|foo |bar | |foobo |foobar |
Are there any modules that permit doing this?
Use Tabulate The first argument of the Tabulate function can transform all the below data types into a table: list of lists or another iterable of iterables. list or another iterable of dicts (keys as columns) dict of iterables (keys as columns)
Answer. HTML table tag is used to display data in tabular form (row * column).
It's not really hard to roll your own formatting function:
def print_table(table): col_width = [max(len(x) for x in col) for col in zip(*table)] for line in table: print "| " + " | ".join("{:{}}".format(x, col_width[i]) for i, x in enumerate(line)) + " |" table = [(str(x), str(f(x))) for x in mylist] print_table(table)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With