这是我在 StackOverflow 上的第一个问题。我很紧张。 我是编程新手,正在尽我最大的努力学习和提高。
我目前正在使用 Beautiful Table 进行一个项目。但是,当我运行我的代码时,我不断收到此弃用警告:
C:\Users\Piko... env\Lib\site-packages eautifultable\utils.py:125: FutureWarning: 'BeautifulTable.getitem' 已在 'v1.0.0' 中弃用,并将在 ' v1.2.0'。使用 'BeautifulTable.{columns|rows}[key]' 代替。
from beautifultable import BeautifulTable
table = BeautifulTable()
table.rows.append([" ", " ", " "])
table.rows.append([" ", " ", " "])
table.rows.append([" ", " ", " "])
table.columns.header = ["A", "B", "C"]
table.rows.header = ["1", "2", "3"]
#below is an example of code that triggers the warning:
table.rows[0]['A'] = "Oops"
我浏览了 BeautifulTable 文档,但似乎无法弄清楚“BeautifulTable.{columns|rows}[key]”的正确语法。
你能帮我重写这段代码,让我不再收到这条警告信息吗?
此外,我使用以下代码作为临时修复,因为警告的红色字体让我很紧张:
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
不要紧张,欢迎来到社区!
我不能直接重现你的问题。
__getitem__
方法在 Python 中被称为 dunder 或 magic 方法。当使用 self[key]
语法访问项目时调用此方法。你可以阅读更多关于这个here.
话虽如此,我没有收到警告:
table.rows[0]['A'] = "Oops"
但是我可以通过以下方式触发警告:
table[0]['A'] = "Oops"
本质上,在第二个代码片段中,我们直接使用 [0] 语法访问表格行(在内部,直接在 beautifultable 对象上调用
__getitem__
)。该警告通知我们此方法已弃用,并将在此包的 v1.2.0 中停止工作。
要停止警告,请在使用方括号表示法之前继续使用语法
table.rows
访问行或 table.columns
访问列。阅读更多这里.
祝你的编码冒险好运!