Python中的Prettytable模块
在本教程中,我们将学习使用Python的Prettytable模块创建关系表。我们将在不使用外部库的情况下创建表格。
什么是Pretty Table
Pretty Table是数据在表格形式下的可视化表示。这些是ASCII表格,易于使用。prettytable库由 PrettyTable 类组成,用于创建关系表。要使用此库,我们需要使用以下命令进行安装。
安装
pip install prettytable
使用Pretty Table创建表格
在这里,我们将使用pretty table模块来创建按行排列的表格。让我们看一下以下示例。
示例1:按行创建表格
from prettytable import PrettyTable
# Creating a new table
newTable = PrettyTable(["Student Name", "Class", "Subject", "Makrs"])
# Add rows
newTable.add_row(["Camron", "X", "English", "91"])
newTable.add_row(["Haris", "X", "Math", "63"])
newTable.add_row(["Jenny", "X", "Science", "90"])
newTable.add_row(["Bernald", "X", "Art", "92"])
newTable.add_row(["Jackson", "X", "Science", "98"])
newTable.add_row(["Samual", "X", "English", "88"])
newTable.add_row(["Stark", "X", "English", "95"])
print(newTable)
输出
+--------------+-------+---------+-------+
| Student Name | Class | Subject | Makrs |
+--------------+-------+---------+-------+
| Camron | X | English | 91 |
| Haris | X | Math | 63 |
| Jenny | X | Science | 90 |
| Bernald | X | Art | 92 |
| Jackson | X | Science | 98 |
| Samual | X | English | 88 |
| Stark | X | English | 95 |
+--------------+-------+---------+-------+
示例2:按列排列的表格
from prettytable import PrettyTable
columns = ["Student Name", "Class", "Subject", "Marks"]
newTable = PrettyTable()
# Add Columns
newTable.add_column(columns[0], ["Jacob", "Peter", "Grenger",
"Stark", "Falcon", "Matthew", "Jackson"])
newTable.add_column(columns[1], ["X", "X", "X", "X", "X", "X", "X"])
newTable.add_column(columns[2], ["English", "Art", "Science", "Math", "Science", "English", "English"])
newTable.add_column(columns[3], ["91", "63", "90", "92",
"98", "83", "95"])
print(newTable)
输出
+--------------+-------+---------+-------------------+
| Student Name | Class | Subject | Marks |
+--------------+-------+---------+-------------------+
| Jacob | X | English | 91 |
| Peter | X | Art | 63 |
| Grenger | X | Science | 90 |
| Stark | X | Math | 92 |
| Falcon | X | Science | 98 |
| Matthew | X | English | 83 |
| Jackson | X | English | 95 |
+--------------+-------+---------+------------------+
我们使用了pretty table模块中的add_column()方法。
示例2:一次添加多行
from prettytable import PrettyTable
# Creating a new table
newTable = PrettyTable(["Student Name", "Class", "Subject", "Makrs"])
# # Add rows
newTable.add_rows(
[
["Camron", "X", "English", "91"],
["Haris", "X", "Math", "63"],
["Jenny", "X", "Science", "90"],
["Bernald", "X", "Art", "92"],
["Jackson", "X", "Science", "98"],
["Samual", "X", "English", "88"],
["Stark", "X", "English", "95"],
]
)
print(newTable)
输出
+--------------+-------+---------+-------+
| Camron | X | English | 91 |
| Haris | X | Math | 63 |
| Jenny | X | Science | 90 |
| Bernald | X | Art | 92 |
| Jackson | X | Science | 98 |
| Samual | X | English | 88 |
| Stark | X | English | 95 |
+--------------+-------+---------+-------+
PrettyTable 删除行
该模块提供了del_row()方法,允许我们删除行。它接受要删除的索引值并删除行。索引从零开始。让我们来了解以下代码。
示例
from prettytable import PrettyTable
# Creating a new table
newTable = PrettyTable(["Student Name", "Class", "Subject", "Makrs"])
# # Add rows
newTable.add_row(["Camron", "X", "English", "91"])
newTable.add_row(["Haris", "X", "Math", "63"])
newTable.add_row(["Jenny", "X", "Science", "90"])
newTable.add_row(["Bernald", "X", "Art", "92"])
newTable.add_row(["Jackson", "X", "Science", "98"])
newTable.add_row(["Samual", "X", "English", "88"])
newTable.add_row(["Stark", "X", "English", "95"])
newTable.del_row(1)
newTable.del_row(2)
newTable.del_row(3)
print(newTable)
输出
+--------------+-------+---------+-------+
| Student Name | Class | Subject | Makrs |
+--------------+-------+---------+-------+
| Camron | X | English | 91 |
| Jenny | X | Science | 90 |
| Jackson | X | Science | 98 |
| Stark | X | English | 95 |
+--------------+-------+---------+-------+
它将从表中删除特定的行。要清除整个表,我们使用以下方法。
myTable.clear_rows()
示例2
from prettytable import PrettyTable
# Creating a new table
newTable = PrettyTable(["Student Name", "Class", "Subject", "Makrs"])
# # Add rows
newTable.add_row(["Camron", "X", "English", "91"])
newTable.add_row(["Haris", "X", "Math", "63"])
newTable.add_row(["Jenny", "X", "Science", "90"])
newTable.add_row(["Bernald", "X", "Art", "92"])
newTable.add_row(["Jackson", "X", "Science", "98"])
newTable.add_row(["Samual", "X", "English", "88"])
newTable.add_row(["Stark", "X", "English", "95"])
newTable.clear_rows()
print(newTable)
输出
+--------------+-------+---------+------------------+
| Student Name | Class | Subject | Makrs |
+--------------+-------+---------+--------------------+
+--------------+-------+---------+--------------------+
如上所示的代码,所有行都已被删除。我们只能看到列名。
获取特定行和列
prettytable 的主要目的是以ASCII形式获取表格。我们可以使用以下方法限制表格的输出。
get_string(fields=["Column 1", "Column2", .............., ColumnN]
让我们来理解以下示例。
示例:
from prettytable import PrettyTable
# Creating a new table
newTable = PrettyTable(["Student Name", "Class", "Subject", "Makrs"])
# # Add rows
newTable.add_rows(
[
["Camron", "X", "English", "91"],
["Haris", "X", "Math", "63"],
["Jenny", "X", "Science", "90"],
["Bernald", "X", "Art", "92"],
["Jackson", "X", "Science", "98"],
["Samual", "X", "English", "88"],
["Stark", "X", "English", "95"],
]
)
print(newTable.get_string(fields=["Student Name", "Class"]))
输出
+--------------+--------------+
| Student Name | Class |
+--------------+--------------+
| Camron | X |
| Haris | X |
| Jenny | X |
| Bernald | X |
| Jackson | X |
| Samual | X |
| Stark | X |
+--------------+---------------+
字段参数采用要打印的字段名列表。
我们还可以使用以下方法来控制结果。
print(newTable.get_string(start=1, end=4)
输出
+--------------+-------+---------+-------------------+
| Student Name | Class | Subject | Makrs |
+--------------+-------+---------+-------------------+
| Camron | X | English | 91 |
| Haris | X | Math | 63 |
| Jenny | X | Science | 90 |
+--------------+-------+---------+------------------+
改变列的对齐方式
我们可以观察到表格中的所有列都居中对齐。根据需要,我们可以通过为属性 align 分配一个字符来进行更改。分别有三个字符串 – ‘l’(代表左对齐),’r’(代表右对齐)和’c’(代表居中对齐)。
让我们看下面的示例。
示例
newTable.align = 'r'
print(newTable)
输出
+--------------+-------+---------+-------------------+
| Student Name | Class | Subject | Makrs |
+--------------+-------+---------+------------------+
| Camron | X | English | 91 |
| Haris | X | Math | 63 |
| Jenny | X | Science | 90 |
| Bernald | X | Art | 92 |
| Jackson | X | Science | 98 |
| Samual | X | English | 88 |
| Stark | X | English | 95 |
+--------------+-------+---------+------------------+
修改表格的外观
默认表格看起来像一个SQL数据库外壳。然而,我们可以以其他格式修改表格的外观。漂亮的表格模块提供了set_style()
方法,将表格转化为适合MSWord的友好格式。
让我们看下面的示例。
示例在创建表格的代码中添加下面的代码。
from prettytable import MSWORD_FRIENDLY
newTable.set_style(MSWORD_FRIENDLY)
print(newTable)
输出
| Student Name | Class | Subject | Makrs |
| Camron | X | English | 91 |
| Haris | X | Math | 63 |
| Jenny | X | Science | 90 |
| Bernald | X | Art | 92 |
| Jackson | X | Science | 98 |
| Samual | X | English | 88 |
| Stark | X | English | 95 |
我们可以清楚地看到默认表格和MS Word友好表格之间的区别。
结论
我们讨论了使用 prettytable 模块在表格中进行一些重要操作。这是使用Python脚本处理表格非常有效的模块。