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

Pet*_* Li 5 python regex warnings pandas

我想使用正则表达式禁止显示特定类型的警告。警告消息是:

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
Run Code Online (Sandbox Code Playgroud)

我抑制过滤器的方式:

import warnings
warnings.filterwarnings("ignore", message= ".*A value is trying to.*")
Run Code Online (Sandbox Code Playgroud)

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

小智 7

您的正则表达式与正确的消息字符串不匹配。

r".*A Value is trying to.*"不匹配,"\nA value is trying to be.*"因为r"."匹配除换行符之外的所有内容

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