📜  使用 PrettyTable 库创建表 - Python

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

使用 PrettyTable 库创建表 - Python

PrettyTable 库中的 PrettyTable类用于在Python中创建关系表。例如,下表是在 Windows 上的命令提示符中使用此库创建的。

安装库:

pip install prettytable 

让我们使用Python中的 prettytable 库创建示例表。

创建表:逐行

Python3
from prettytable import PrettyTable
  
# Specify the Column Names while initializing the Table
myTable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
  
# Add rows
myTable.add_row(["Leanord", "X", "B", "91.2 %"])
myTable.add_row(["Penny", "X", "C", "63.5 %"])
myTable.add_row(["Howard", "X", "A", "90.23 %"])
myTable.add_row(["Bernadette", "X", "D", "92.7 %"])
myTable.add_row(["Sheldon", "X", "A", "98.2 %"])
myTable.add_row(["Raj", "X", "B", "88.1 %"])
myTable.add_row(["Amy", "X", "B", "95.0 %"])
  
print(myTable)


Python3
from prettytable import PrettyTable
  
columns = ["Student Name", "Class", "Section", "Percentage"]
  
myTable = PrettyTable()
  
# Add Columns
myTable.add_column(columns[0], ["Leanord", "Penny", "Howard",
                       "Bernadette", "Sheldon", "Raj", "Amy"])
myTable.add_column(columns[1], ["X", "X", "X", "X", "X", "X", "X"])
myTable.add_column(columns[2], ["B", "C", "A", "D", "A", "B", "B"])
myTable.add_column(columns[3], ["91.2 %", "63.5 %", "90.23 %", "92.7 %", 
                                          "98.2 %", "88.1 %", "95.0 %"])
  
print(myTable)



输出
+--------------+-------+---------+------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
|   Leanord    |   X   |    B    |   91.2%    |
|    Penny     |   X   |    C    |   63.5%    |
|    Howard    |   X   |    A    |   90.23%   |
|  Bernadette  |   X   |    D    |   92.7%    |
|   Sheldon    |   X   |    A    |   98.2%    |
|     Raj      |   X   |    B    |   88.1%    |
|     Amy      |   X   |    B    |   95.0%    |
+--------------+-------+---------+------------+

创建表:按列

Python3

from prettytable import PrettyTable
  
columns = ["Student Name", "Class", "Section", "Percentage"]
  
myTable = PrettyTable()
  
# Add Columns
myTable.add_column(columns[0], ["Leanord", "Penny", "Howard",
                       "Bernadette", "Sheldon", "Raj", "Amy"])
myTable.add_column(columns[1], ["X", "X", "X", "X", "X", "X", "X"])
myTable.add_column(columns[2], ["B", "C", "A", "D", "A", "B", "B"])
myTable.add_column(columns[3], ["91.2 %", "63.5 %", "90.23 %", "92.7 %", 
                                          "98.2 %", "88.1 %", "95.0 %"])
  
print(myTable)


输出
+--------------+-------+---------+------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
|   Leanord    |   X   |    B    |   91.2%    |
|    Penny     |   X   |    C    |   63.5%    |
|    Howard    |   X   |    A    |   90.23%   |
|  Bernadette  |   X   |    D    |   92.7%    |
|   Sheldon    |   X   |    A    |   98.2%    |
|     Raj      |   X   |    B    |   88.1%    |
|     Amy      |   X   |    B    |   95.0%    |
+--------------+-------+---------+------------+

删除行

myTable.del_row(0)

这将从表中删除第一行,即这些行遵循从索引 0 开始的标准索引。

清理桌子

myTable.clear_rows()

这将清除整个表格(仅保留列名)。

有许多与这些表格相关的高级功能,例如将这些表格转换为 HTML 或将 CSV 转换为 PrettyTable。这些功能将在单独的文章中介绍。