gpt4 book ai didi

python - 为什么我不能使用 warnings.filterwarnings 抑制正则表达式的警告

转载 作者:太空狗 更新时间:2023-10-30 01:37:13 24 4
gpt4 key购买 nike

我想使用正则表达式抑制特定类型的警告。警告信息是:

C:\Anaconda3\lib\site-packages\pandas\core\indexing.py:420: SettingWithCopyWarning:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item] = s

我的抑制过滤方式:

import warnings
warnings.filterwarnings("ignore", message= ".*A value is trying to.*")

然而,它失败了。我确实尝试将警告消息的不同部分粘贴到正则表达式中,但仍然失败。我想知道为什么。

最佳答案

您的正则表达式不匹配the correct message string .

r".*A Value is trying to.*" does not match "\nA value is trying to be.*" 因为r"。 " 匹配所有除了换行符

有时,如果不查看生成警告的模块的源代码,就很难弄清楚实际的消息字符串是什么。

关于python - 为什么我不能使用 warnings.filterwarnings 抑制正则表达式的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39342443/

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