You can combine the usage of zip() with tabulate to create a nicer looking table:
from tabulate import tabulate
headers = ['planet', 'amp', 'mass', 'period', 'ecc']
amp = [1.1, 1.2, 1.3, 1.4]
mass = [2.1, 2.2, 2.3, 2.4]
period = [3.1, 3.2, 3.3, 3.4]
ecc = [4.1, 4.2, 4.3, 4.4]
planet = range(1, len(amp)+1)
table = zip(planet, amp, mass, period, ecc)
print(tabulate(table, headers=headers, floatfmt=".4f"))
Output:
planet amp mass period ecc
-------- ------ ------ -------- ------
1 1.1000 2.1000 3.1000 4.1000
2 1.2000 2.2000 3.2000 4.2000
3 1.3000 2.3000 3.3000 4.3000
4 1.4000 2.4000 3.4000 4.4000
Answer from Liran Funaro on Stack Overflow
» pip install tabulate
Videos
You can combine the usage of zip() with tabulate to create a nicer looking table:
from tabulate import tabulate
headers = ['planet', 'amp', 'mass', 'period', 'ecc']
amp = [1.1, 1.2, 1.3, 1.4]
mass = [2.1, 2.2, 2.3, 2.4]
period = [3.1, 3.2, 3.3, 3.4]
ecc = [4.1, 4.2, 4.3, 4.4]
planet = range(1, len(amp)+1)
table = zip(planet, amp, mass, period, ecc)
print(tabulate(table, headers=headers, floatfmt=".4f"))
Output:
planet amp mass period ecc
-------- ------ ------ -------- ------
1 1.1000 2.1000 3.1000 4.1000
2 1.2000 2.2000 3.2000 4.2000
3 1.3000 2.3000 3.3000 4.3000
4 1.4000 2.4000 3.4000 4.4000
Use zip() like this:
amp = ['a1', 'a2', 'a3', 'a4']
mass = ['m1', 'm2', 'm3', 'm4']
period = ['p1', 'p2', 'p3', 'p4']
ecc = ['e1', 'e2', 'e3', 'e4']
planet = [1, 2, 3, 4]
titles = ['planet', 'amp', 'mass', 'period', 'ecc']
print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*titles)
for item in zip(planet, amp, mass, period, ecc):
print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*item)
Instead of using planet = [1, 2, 3, 4], you can use enumerate() like below:
print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*titles)
for i, item in enumerate(zip(amp, mass, period, ecc)):
print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(i+1, *item)
Output:
>>> python print_table.py
planet|amp |mass |period|ecc
1 |a1 |m1 |p1 |e1
2 |a2 |m2 |p2 |e2
3 |a3 |m3 |p3 |e3
4 |a4 |m4 |p4 |e4
There are some light and useful python packages for this purpose:
1. tabulate
https://pypi.python.org/pypi/tabulate
from tabulate import tabulate
print(tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age']))
Name Age
------ -----
Alice 24
Bob 19
tabulate has many options to specify headers and table format.
print(tabulate(
[['Alice', 24], ['Bob', 19]],
headers=['Name', 'Age'],
tablefmt='orgtbl'))
| Name | Age |
|--------+-------|
| Alice | 24 |
| Bob | 19 |
2. PrettyTable
https://pypi.python.org/pypi/PrettyTable
from prettytable import PrettyTable
t = PrettyTable(['Name', 'Age'])
t.add_row(['Alice', 24])
t.add_row(['Bob', 19])
print(t)
+-------+-----+
| Name | Age |
+-------+-----+
| Alice | 24 |
| Bob | 19 |
+-------+-----+
PrettyTable has options to read data from csv, html, sql database. Also you are able to select subset of data, sort table and change table styles.
3. texttable
https://pypi.python.org/pypi/texttable
from texttable import Texttable
t = Texttable()
t.add_rows([['Name', 'Age'], ['Alice', 24], ['Bob', 19]])
print(t.draw())
+-------+-----+
| Name | Age |
+=======+=====+
| Alice | 24 |
+-------+-----+
| Bob | 19 |
+-------+-----+
with texttable you can control horizontal/vertical align, border style and data types.
4. termtables
https://github.com/nschloe/termtables
import termtables as tt
string = tt.to_string(
[["Alice", 24], ["Bob", 19]],
header=["Name", "Age"],
style=tt.styles.ascii_thin_double,
# alignment="ll",
# padding=(0, 1),
)
print(string)
+-------+-----+
| Name | Age |
+=======+=====+
| Alice | 24 |
+-------+-----+
| Bob | 19 |
+-------+-----+
with texttable you can control horizontal/vertical align, border style and data types.
Other options
- terminaltables - Easily draw tables in terminal/console applications from a list of lists of strings. Supports multi-line rows.
- asciitable can read and write a wide range of ASCII table formats via built-in Extension Reader Classes.
Some ad-hoc code:
row_format ="{:>15}" * (len(teams_list) + 1)
print(row_format.format("", *teams_list))
for team, row in zip(teams_list, data):
print(row_format.format(team, *row))
This relies on str.format() and the Format Specification Mini-Language.