gpt4 book ai didi

python - 使用 prettytable 时如何获取单元格的值

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:55 41 4
gpt4 key购买 nike

使用prettytable时是否可以获取特定单元格的值? ?

我有以下代码来遍历一个简单表格的所有行。

from prettytable import PrettyTable

table = PrettyTable(["Column 1", "Column 2", "Column 3"])
table.add_row(["A", "B", "C"])
table.add_row(["F", "O", "O"])
table.add_row(["B", "A", "R"])

for row in table:
print(row)

此示例打印以下 3 个表:

+----------+----------+----------+
| Column 1 | Column 2 | Column 3 |
+----------+----------+----------+
| A | B | C |
+----------+----------+----------+
+----------+----------+----------+
| Column 1 | Column 2 | Column 3 |
+----------+----------+----------+
| F | O | O |
+----------+----------+----------+
+----------+----------+----------+
| Column 1 | Column 2 | Column 3 |
+----------+----------+----------+
| B | A | R |
+----------+----------+----------+

如何只获取一行的Column 1Column 2Column 3 的值?

最佳答案

看起来您想要做的是从 documentation here 中获取数据的子集。 , 尝试:

from prettytable import PrettyTable

table = PrettyTable(["Column 1", "Column 2", "Column 3"])
table.add_row(["A", "B", "C"])
table.add_row(["F", "O", "O"])
table.add_row(["B", "A", "R"])

for row in table:
print row.get_string(fields=["Column 1"]) # Column 1

编辑:看起来您不需要标题或边框,只需要值,在这种情况下,这应该可行:

from prettytable import PrettyTable

table = PrettyTable(["Column 1", "Column 2", "Column 3"])
table.add_row(["A", "B", "C"])
table.add_row(["F", "O", "O"])
table.add_row(["B", "A", "R"])

for row in table:
row.border = False
row.header = False
print row.get_string(fields=["Column 1"]).strip() # Column 1

关于python - 使用 prettytable 时如何获取单元格的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24365655/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com