rohan avatar

Create readable CLI table using Python's PrettyTable library

rohancmr

Published: 10 Jan 2018 › Updated: 10 Jan 2018Create readable CLI table using Python's PrettyTable library

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

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

Leave Create readable CLI table using Python's PrettyTable library to:

Written by

Read more #utopian-io posts


Best Posts From rohan

We have not curated any of rohancmr's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From rohan