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.
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.
Best tool for printing tables in Jupyter notebook?
How to print data in a table format?
Hey, i'm a beginner in python and it's been only couple of weeks since i've been into the world of coding. What I want to do is to print a table, by a table I mean a list that contains sublists and I want them to be printed in a form of a table, when printing it using the following code :
for i in range(n+1):
print(*lst[i],end='')
print()The printed table looks like :
1 2 89 92 4 56 84 144 1 5 0 6 6 63 173 242
My concern here is that I want it to be printed in a more organized way, so that it would like :
1 2 89 92 4 56 84 144 1 5 0 6 6 63 173 242
Any help will be appreciated ! Thanks in advance !
Hi Guys,
Please help me out, I am very weak in programming. I have the below code block which prints out all EC2 instances in an AWS account that are not of the latest generation along with the Cloudformation template used to provision them. The code is working fine now, it's just I need the output to be in a table. I tried using tabulate (commented out in the code block below) but the problem is it prints out and repeats the "headers" specified in each row instead of just on the very top, i think this is because of the whole thing is wrapped in for loop, not sure how I can work around it. I was told that the csv module maybe helpful but I looked python doc and it looks like that is for reading data from a .csv file and then processing it using the csv module. If you guyz can edit my code below and paste it here with the modification needed to display the data in a table format, I would be ever grateful to you. Please help me :( :( :(
import boto3
from tabulate import tabulate
ec2 = boto3.resource('ec2')
latestInstanceTypes = ['t3', 'm5', 'c5']
for instance in ec2.instances.all():
if instance.instance_type.split('.')[0] not in latestInstanceTypes:
stack_ids = [x['Value'] for x in instance.tags if x['Key'] == 'aws:cloudformation:stack-id']
stack_id = stack_ids[0] if len(stack_ids) else None
instance_names = [x['Value'] for x in instance.tags if x['Key'] == 'Name']
instance_name = instance_names[0] if len(instance_names) else None
#print(tabulate([[instance.id, instance.platform, instance.instance_type, instance.public_ip_address, instance.image.id, instance.state, stack_id]], headers=['Id', 'Platform', 'Type', 'Public IPv4', 'AMI', 'State', 'CF Template'], tablefmt='orgtbl'))
print(
"Id: {0}\nName: {1}\nType: {2}\nCFTemplate: {3}\nPlatform: {4}\nAMI: {5}\nState: {6}\n".format(
instance.id, instance_name, instance.instance_type, stack_id, instance.platform, instance.image.id, instance.state
)
)