您的位置:

Python PrettyTable 模块

在本教程中,我们将学习使用 Python PrettyTable 模块创建一个关系表。我们将在不使用外部库的情况下创建表。

什么是漂亮的桌子?

漂亮的表格是表格形式的数据的可视化表示。这些是 ASCII 表,很容易使用。可修改库由可修改类组成,用于创建关系表。要使用这个库,我们需要使用下面的命令安装它。

装置


pip install prettytable

使用漂亮的表格创建表格

在这里,我们将使用漂亮的表模块创建行方式的表。让我们理解下面的例子。

示例- 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    |
+--------------+-------+---------+------------------+

我们使用了漂亮表模块的 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  |
+--------------+-------+---------+-------+

可修改的删除行

这个模块提供了 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 |
+--------------+-------+---------+--------------------+
+--------------+-------+---------+--------------------+

正如我们在上面的代码中看到的,所有的行都被删除了。我们只能看到列名。

获取特定的行和列

可修改的主要目的是得到 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   |
+--------------+---------------+

field 参数接受要打印的字段名列表。

我们也可以用下面的方法来控制结果。


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  |
+--------------+-------+---------+------------------+

更改列的对齐方式

我们可以观察到表中的所有列都居中对齐。我们可以根据需要改变,给分配一个字符,对齐属性。有三个字符串——“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 数据库 Shell。但是,我们可以用其他格式修改表格外观。漂亮的表格模块提供了 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  |

我们可以清楚地看到默认表和 MSWord 友好表的区别。

结论

我们已经使用可修改的模块讨论了表格中的一些重要操作。这是一个使用 Python 脚本处理表的非常有效的模块。