Create readable CLI table using Python's PrettyTable library
PrettyTable is a python library designed to display tabular data in a visually appealing ASCII table. It generates tables similar to one used in PostgreSQL shell psql.
Features of PrettyTable:
- Independent alignment of columns (left or right justified or centered).
- Print sub-tables by specifying a row range.
- Control aspects of a table such as the width of column and table border.
- Sort data based on a column.
- Read data from python variable, CSV, HTML or database cursor.
- Output data in ASCII or HTML.
Installation
PrettyTable can be installed using pip. In Windows, pip is available under "script" directory and in linux under "bin" directory of Python's installation path.
Command to install PrettyTable using pip :
pip install prettytable
Once it is installed, check if we can import prettytable using below command :
import prettytable
Example 1 : Create a table using add_row() method
- To create a table using add_row() method, import PrettyTable from prettytable :
from prettytable import PrettyTable
- Set the header name for the table
table = PrettyTable(['Coin', 'Price', 'High', 'Low'])
- Add each row of table rows using add_row() function
table.add_row(['BTC', '14525.00 USD', '15355.00 USD', '13755.00 USD'])
table.add_row(['ETH', '1191.00 USD', '1250.00 USD', '965.18 USD'])
table.add_row(['XRP', '2.25 USD', '2.49 USD', '1.90 USD'])
table.add_row(['LTC', '247.72 USD', '258.04 USD', '230.18 USD'])
table.add_row(['MIOTA', '3.64 USD', '3.95 USD', '3.15 USD'])
- Print the table
print(table)
Full Code
- Save the script and run it. (I have saved it as print_table.py)
Example 2 : Create a table from a CSV file :
- Content of CSV
- To create a table using csv, import
from_csv()
method from prettytable.
from prettytable import from_csv
Open the CSV file in read mode.
with open('data.csv', 'r') as f:
Read the content of CSV file. Then use
from_csv()
method on the read content and store it in a variable.
table = from_csv(f)
Set the columns to left justified.
table.align = 'l'
Print the table.
print(table)
Full Code :
- Save the script and run it. (I have saved it as print_csv.py)
Example 3 : Create table from a html file.
- Content of data.html file.
- To create a table from a html file, import
from_html_one()
method from prettytable.
from prettytable import from_html_one
- Open the html file in read mode.
with open('data.html', 'r') as f:
- Read the content of html file and store it in a variable.
html_data = f.read()
- Use the
from_html_one()
method and pass the read html content and store it in a variable.
table = from_html_one(html_data)
- Set the columns to left justified.
table.align = 'l'
- Print the table.
print(table)
Full Code
- Save the script and run it. (I have saved it as print_html.py)
Conclusion
Using PrettyTable python library we can quickly and easily represent tabular data in nice CLI tables. Apart from above examples, it can be used for printing range of rows or selected columns. It can also be used to display table by sorting a given column.
If you have any questions, comments or have used PrettyTable before, I'd would love to hear from you in comment section.
Posted on Utopian.io - Rewarding Open Source Contributors
Hey @rohancmr I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Nice post i like it
I follow you and you follow mee back
i vote you vote me back
Thank you.
@originalworks
The @OriginalWorks bot has determined this post by @rohancmr to be original material and upvoted it!
To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!
Nice post @rohancmr. Impressive tool.
Good how to. Upvote and Follow!
Just click (^) icon to upvote and click my user ID "rohancmr" and then click the follow link.
Already done !
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]