Create readable CLI table using Python's PrettyTable library

in #utopian-io7 years ago (edited)

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

install.gif

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

print_table.PNG

  • Save the script and run it. (I have saved it as print_table.py)

table_print.gif

Example 2 : Create a table from a CSV file :

  • Content of CSV

data_csv.PNG

  • 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 :

print_csv.PNG

  • Save the script and run it. (I have saved it as print_csv.py)

from_csv.gif

Example 3 : Create table from a html file.

  • Content of data.html file.

datahtml.PNG

  • 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

from_html.PNG

  • Save the script and run it. (I have saved it as print_html.py)

from_html.gif

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

Sort:  

Hey @rohancmr I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • This is your first accepted contribution here in Utopian. Welcome!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

The @OriginalWorks bot has determined this post by @rohancmr to be original material and upvoted it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!

Please note that this is a BETA version. Feel free to leave a reply if you feel this is an error to help improve accuracy.

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]

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.031
BTC 60836.17
ETH 2567.48
USDT 1.00
SBD 2.57