Controlling which data gets displayed

You can control which rows or which columns are going to be displayed.
 

Python3




# importing required library
from prettytable import PrettyTable
 
# creating an empty PrettyTable
x = PrettyTable()
 
# adding data into the table
# column by column
x.field_names = ["First name", "Last name", "Salary", "City", "DOB"]
x.add_row(["Shubham", "Chauhan", 60000, "Lucknow", "22 Feb 1999"])
x.add_row(["Saksham", "Chauhan", 50000, "Hardoi", "21 Aug 2000"])
x.add_row(["Preeti", "Singh", 40000, "Unnao", "10 Jan 1995"])
x.add_row(["Ayushi", "Chauhan", 65000, "Haridwar", "30 Jan 2002"])
x.add_row(["Abhishek", "Rai", 70000, "Greater Noida", "16 Jan 1999"])
x.add_row(["Dinesh", "Pratap", 80000, "Delhi", "3 Aug 1998"])
x.add_row(["Chandra", "Kant", 85000, "Ghaziabad", "18 Sept 1997"])
 
# With the start and end parameters, we can select
# which rows to display in the output.
print(x.get_string(start=2, end=4))
 
# With the fields option we can select columns
# which are going to be displayed.
print(x.get_string(fields=["First name", "Salary", "City"]))


Output:

Various other operations which can be performed

  • Sorting(ascending or descending) can be performed using the sortby property, in which we sort the table by specifying a column to be sorted.
  • HTML output of the table can be generated using the get_html_string method.

Python3




# importing required library
from prettytable import PrettyTable
 
# creating an empty PrettyTable
x = PrettyTable()
 
# adding data into the table
# row by row
x.field_names = ["First name", "Last name", "Salary", "City", "DOB"]
x.add_row(["Shubham", "Chauhan", 60000, "Lucknow", "22 Feb 1999"])
x.add_row(["Saksham", "Chauhan", 50000, "Hardoi", "21 Aug 2000"])
x.add_row(["Preeti", "Singh", 40000, "Unnao", "10 Jan 1995"])
x.add_row(["Ayushi", "Chauhan", 65000, "Haridwar", "30 Jan 2002"])
x.add_row(["Abhishek", "Rai", 70000, "Greater Noida", "16 Jan 1999"])
x.add_row(["Dinesh", "Pratap", 80000, "Delhi", "3 Aug 1998"])
x.add_row(["Chandra", "Kant", 85000, "Ghaziabad", "18 Sept 1997"])
 
# printing generated table
print("Original Table:")
print(x)
 
# printing the HTML string of this table
print("\nHTML code for this Table:")
print(x.get_html_string())
 
# printing table after sorting(ascending)
# by column salary
x.sortby = "Salary"
print("\nSorted Table by Salary:")
print(x.get_string())
 
# printing table after sorting(ascending)
# by column city
x.sortby = "City"
x.reversesort = True
print("\nReverse Sorted Table by City:")
print(x.get_string())


Output:



Generate simple ASCII tables using prettytable in Python

Prettytable is a Python library used to print ASCII tables in an attractive form and to read data from CSV, HTML, or database cursor and output data in ASCII or HTML. We can control many aspects of a table, such as the width of the column padding, the alignment of text, or the table border.
 

Installation 

In order to be able to use prettytable library first we need to install it using pip tool command:

pip install prettytable

Similar Reads

Inserting data

Data can be inserted in a PrettyTable either row by row or column by column....

Deleting Data

...

Controlling which data gets displayed

...