📜  在Python中使用 prettytable 生成简单的 ASCII 表

📅  最后修改于: 2022-05-13 01:55:45.736000             🧑  作者: Mango

在Python中使用 prettytable 生成简单的 ASCII 表

Prettytable 是一个Python库,用于以有吸引力的形式打印 ASCII 表,并从 CSV、HTML 或数据库光标读取数据并以 ASCII 或 HTML 格式输出数据。我们可以控制表格的许多方面,例如列填充的宽度、文本的对齐方式或表格边框。

安装

为了能够首先使用漂亮的库,我们需要
使用 pip 工具命令安装它:

pip install prettytable

插入数据

数据可以逐行逐列插入到 PrettyTable 中。

逐行插入数据

为此,您可以先使用 field_names 属性设置字段名称,然后使用 add_row 方法一次添加一行。
例子:

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(x)


Python3
# importing required library
from prettytable import PrettyTable
  
# creating an empty PrettyTable
x = PrettyTable()
  
# adding data into the table
# column by column
x.add_column("First name",
             ["Shubham", "Saksham", "Preeti", "Ayushi",
              "Abhishek", "Dinesh", "Chandra"])
  
x.add_column("Last name", ["Chauhan", "Chauhan", "Singh",
                           "Chauhan", "Rai", "Pratap",
                           "Kant"])
  
x.add_column("Salary", [60000, 50000, 40000, 65000, 70000,
                        80000, 85000])
x.add_column("City", ["Lucknow", "Hardoi", "Unnao", "Haridwar",
                      "Greater Noida", "Delhi", "Ghaziabad"])
  
x.add_column("DOB", ["22 Feb 1999", "21 Aug 2000", "10 Jan 1995",
                     "30 Jan 2002", "16 Jan 1999", "3 Aug 1998",
                     "18 Sept 1997"])
  
# printing generated table
print(x)


Python3
# importing required library
from prettytable import PrettyTable
  
# creating an empty PrettyTable
x = PrettyTable()
  
# adding data into the table
# column by column
x.add_column("First name",
             ["Shubham", "Saksham", "Preeti", "Ayushi",
              "Abhishek", "Dinesh", "Chandra"])
  
x.add_column("Last name", ["Chauhan", "Chauhan", "Singh",
                           "Chauhan", "Rai", "Pratap",
                           "Kant"])
  
x.add_column("Salary", [60000, 50000, 40000, 65000, 70000,
                        80000, 85000])
x.add_column("City", ["Lucknow", "Hardoi", "Unnao", "Haridwar",
                      "Greater Noida", "Delhi", "Ghaziabad"])
  
x.add_column("DOB", ["22 Feb 1999", "21 Aug 2000", "10 Jan 1995",
                     "30 Jan 2002", "16 Jan 1999", "3 Aug 1998",
                     "18 Sept 1997"])
  
# Deleting row
x.del_row(2)
x.del_row(3)
  
# printing generated table
print(x)


Python3
# importing required library
from prettytable import PrettyTable
  
# creating an empty PrettyTable
x = PrettyTable()
  
# adding data into the table
# column by column
x.add_column("First name",
             ["Shubham", "Saksham", "Preeti", "Ayushi",
              "Abhishek", "Dinesh", "Chandra"])
  
x.add_column("Last name", ["Chauhan", "Chauhan", "Singh",
                           "Chauhan", "Rai", "Pratap",
                           "Kant"])
  
x.add_column("Salary", [60000, 50000, 40000, 65000, 70000,
                        80000, 85000])
x.add_column("City", ["Lucknow", "Hardoi", "Unnao", "Haridwar",
                      "Greater Noida", "Delhi", "Ghaziabad"])
  
x.add_column("DOB", ["22 Feb 1999", "21 Aug 2000", "10 Jan 1995",
                     "30 Jan 2002", "16 Jan 1999", "3 Aug 1998",
                     "18 Sept 1997"])
  
# Deleting all rows
x.clear_rows()
  
print(x)
  
# Deleting column name as well
print("\nAfter clearing column names as well")
x.clear()
  
print(x)


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"]))


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())


输出:

逐列插入数据

为此,您使用 add_column 方法,该方法接受两个参数 - 一个字符串,它是您要添加的列对应的字段的名称,以及一个包含列数据的列表或元组”
例子:

Python3

# importing required library
from prettytable import PrettyTable
  
# creating an empty PrettyTable
x = PrettyTable()
  
# adding data into the table
# column by column
x.add_column("First name",
             ["Shubham", "Saksham", "Preeti", "Ayushi",
              "Abhishek", "Dinesh", "Chandra"])
  
x.add_column("Last name", ["Chauhan", "Chauhan", "Singh",
                           "Chauhan", "Rai", "Pratap",
                           "Kant"])
  
x.add_column("Salary", [60000, 50000, 40000, 65000, 70000,
                        80000, 85000])
x.add_column("City", ["Lucknow", "Hardoi", "Unnao", "Haridwar",
                      "Greater Noida", "Delhi", "Ghaziabad"])
  
x.add_column("DOB", ["22 Feb 1999", "21 Aug 2000", "10 Jan 1995",
                     "30 Jan 2002", "16 Jan 1999", "3 Aug 1998",
                     "18 Sept 1997"])
  
# printing generated table
print(x)

输出:

删除数据

可以使用 del_row() 方法删除特定行。要删除的行的索引作为参数传递。

例子:

Python3

# importing required library
from prettytable import PrettyTable
  
# creating an empty PrettyTable
x = PrettyTable()
  
# adding data into the table
# column by column
x.add_column("First name",
             ["Shubham", "Saksham", "Preeti", "Ayushi",
              "Abhishek", "Dinesh", "Chandra"])
  
x.add_column("Last name", ["Chauhan", "Chauhan", "Singh",
                           "Chauhan", "Rai", "Pratap",
                           "Kant"])
  
x.add_column("Salary", [60000, 50000, 40000, 65000, 70000,
                        80000, 85000])
x.add_column("City", ["Lucknow", "Hardoi", "Unnao", "Haridwar",
                      "Greater Noida", "Delhi", "Ghaziabad"])
  
x.add_column("DOB", ["22 Feb 1999", "21 Aug 2000", "10 Jan 1995",
                     "30 Jan 2002", "16 Jan 1999", "3 Aug 1998",
                     "18 Sept 1997"])
  
# Deleting row
x.del_row(2)
x.del_row(3)
  
# printing generated table
print(x)

输出:

可以使用 clear_rows() 方法删除表中的所有行。此方法将保留列名。要删除行和列的名称,请使用 clear() 方法。

例子:

Python3

# importing required library
from prettytable import PrettyTable
  
# creating an empty PrettyTable
x = PrettyTable()
  
# adding data into the table
# column by column
x.add_column("First name",
             ["Shubham", "Saksham", "Preeti", "Ayushi",
              "Abhishek", "Dinesh", "Chandra"])
  
x.add_column("Last name", ["Chauhan", "Chauhan", "Singh",
                           "Chauhan", "Rai", "Pratap",
                           "Kant"])
  
x.add_column("Salary", [60000, 50000, 40000, 65000, 70000,
                        80000, 85000])
x.add_column("City", ["Lucknow", "Hardoi", "Unnao", "Haridwar",
                      "Greater Noida", "Delhi", "Ghaziabad"])
  
x.add_column("DOB", ["22 Feb 1999", "21 Aug 2000", "10 Jan 1995",
                     "30 Jan 2002", "16 Jan 1999", "3 Aug 1998",
                     "18 Sept 1997"])
  
# Deleting all rows
x.clear_rows()
  
print(x)
  
# Deleting column name as well
print("\nAfter clearing column names as well")
x.clear()
  
print(x)

输出:

控制显示哪些数据

您可以控制要显示哪些行或哪些列。

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"]))

输出:

可以执行的各种其他操作

  • 可以使用 sortby 属性执行排序(升序或降序),其中我们通过指定要排序的列来对表进行排序。
  • 可以使用 get_html_string 方法生成表格的 HTML 输出。

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())

输出: