In this article we look at a library that displays tabular data into ASCII tables called Prettytable
installation
As always you can install this from pypi like this
pip install prettytable
Example
Lets see some examples of this library in action
Basic example
In this example we display some countries, capitals of them and the continent they are in
from prettytable import PrettyTable # Creating a new table newTable = PrettyTable(["Country", "Capital", "Continent"]) # Add rows newTable.add_row(["France", "Paris", "Europe"]) newTable.add_row(["Germany", "Berlin", "Europe"]) newTable.add_row(["Japan", "Tokyo", "Asia"]) newTable.add_row(["Peru", "Lima", "South America"]) newTable.add_row(["Egypyt", "Cairo", "Africa"]) newTable.add_row(["China", "Beijing", "Asia"]) print(newTable)
When run you should see something that looks a bit like this
>>> %Run pretttablebasic.py +---------+---------+---------------+ | Country | Capital | Continent | +---------+---------+---------------+ | France | Paris | Europe | | Germany | Berlin | Europe | | Japan | Tokyo | Asia | | Peru | Lima | South America | | Egypyt | Cairo | Africa | | China | Beijing | Asia | +---------+---------+---------------+
Import from CSV
You can also import a csv file and display the data in a Prettytable, this data is from another example when we were creating csv files in python
The data we have in the csv file is
Entry,Country,Capital
1,France,Paris
2,Germany,Berlin
3,Spain,Madrid
4,Italy,Rome
5,UK,London
Lets now display it
from prettytable import from_csv with open("countries.csv") as fp: mytable = from_csv(fp) print(mytable)
When run this displayed the following
>>> %Run perttytablefromcsv.py +-------+---------+---------+ | Entry | Country | Capital | +-------+---------+---------+ | 1 | France | Paris | | 2 | Germany | Berlin | | 3 | Spain | Madrid | | 4 | Italy | Rome | | 5 | UK | London | +-------+---------+---------+
Adding data by columns
The first example we added data by row, you can also add it by column if you desire, like this
from prettytable import PrettyTable columns = ["Country", "Capital", "Continent"] newTable = PrettyTable() # Add Columns newTable.add_column(columns[0], ["France", "Germany", "Japan", "Peru", "Egypt", "China"]) newTable.add_column(columns[1], ["Paris", "Berlin", "Tokyo", "Lima", "Cairo", "Beijing"]) newTable.add_column(columns[2], ["Europe", "Europe", "Asia", "South America", "Africa", "Asia"]) print(newTable)
When run, as expected the results are like this
>>> %Run prettytablecolumns.py +---------+---------+---------------+ | Country | Capital | Continent | +---------+---------+---------------+ | France | Paris | Europe | | Germany | Berlin | Europe | | Japan | Tokyo | Asia | | Peru | Lima | South America | | Egypt | Cairo | Africa | | China | Beijing | Asia | +---------+---------+---------------+
Deleting rows
We can also delete rows, take this example where we add the rows, print the table and then we delete rows we do not want and print the table again.
from prettytable import PrettyTable # Creating a new table newTable = PrettyTable(["Country", "Capital", "Continent"]) # Add rows newTable.add_row(["France", "Paris", "Europe"]) newTable.add_row(["Germany", "Berlin", "Europe"]) newTable.add_row(["Japan", "Tokyo", "Asia"]) newTable.add_row(["Peru", "Lima", "South America"]) newTable.add_row(["Egypt", "Cairo", "Africa"]) newTable.add_row(["China", "Beijing", "Asia"]) print(newTable) newTable.del_row(1) newTable.del_row(2) newTable.del_row(3) print(newTable)
Running this and you will see the following
>>> %Run prettytabledeleterow.py +---------+---------+---------------+ | Country | Capital | Continent | +---------+---------+---------------+ | France | Paris | Europe | | Germany | Berlin | Europe | | Japan | Tokyo | Asia | | Peru | Lima | South America | | Egypt | Cairo | Africa | | China | Beijing | Asia | +---------+---------+---------------+ +---------+---------+-----------+ | Country | Capital | Continent | +---------+---------+-----------+ | France | Paris | Europe | | Japan | Tokyo | Asia | | Egypt | Cairo | Africa | +---------+---------+-----------+
Clear the entire table
You can clear the entire table with the clear_rows() method
from prettytable import PrettyTable # Creating a new table newTable = PrettyTable(["Country", "Capital", "Continent"]) # Add rows newTable.add_row(["France", "Paris", "Europe"]) newTable.add_row(["Germany", "Berlin", "Europe"]) newTable.add_row(["Japan", "Tokyo", "Asia"]) newTable.add_row(["Peru", "Lima", "South America"]) newTable.add_row(["Egypyt", "Cairo", "Africa"]) newTable.add_row(["China", "Beijing", "Asia"]) newTable.clear_rows() print(newTable)
When run this displays the following
>>> %Run prettytableclear.py +---------+---------+-----------+ | Country | Capital | Continent | +---------+---------+-----------+ +---------+---------+-----------+
There are several ways to get data out of a PrettyTable
- The del_row method takes an integer index of a single row to delete.
- The del_column method takes a field name of a single column to delete.
- The clear_rows method takes no arguments and deletes all the rows in the table – but keeps the field names as they were so you that you can repopulate it with the same kind of data.
- The clear method takes no arguments and deletes all rows and all field names. It's not quite the same as creating a fresh table instance, though – style related settings, discussed later, are maintained.
Links
the github page of the library