为什么我不能使用 warnings.filterwarnings 抑制正则表达式的警告
why I cannot suppress warning with regex using warnings.filterwarnings
我想使用正则表达式抑制特定类型的警告。
警告信息是:
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.*"
不匹配 "\nA value is trying to be.*"
因为 r"."
匹配所有 除了换行符 .
有时不查看生成警告的模块的源代码就很难弄清楚实际的消息字符串是什么。
这不是 filterwarnings 的工作方式。在文档中您可以看到 Omitted arguments default to a value that matches everything.
,也可以看到:message (default '') : is a string containing a regular expression that start of the warning message must match
。
这可以理解为使用动作"once"会影响每条唯一的短信显示一次。如果您在消息中有一个可能会更改的字段(例如文件名),则警告将针对每个文件名显示一次。
如果您设置消息参数,每个匹配的唯一文本消息将显示一次。
这是一个小例子:
import warnings
import random
warnings.filterwarnings("ignore", category=UserWarning)
warnings.warn("You won't see this warning")
message_formater = "this message with number {} will be displayed once"
# deactivating previously created filter
warnings.resetwarnings()
# applying new filter
warnings.filterwarnings("once", message_formater.format("(.*)"), category=UserWarning)
for i in range(100):
warnings.warn(message_formater.format(random.randint(0, 3)))
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 0 will be displayed once
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 3 will be displayed once
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 1 will be displayed once
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 2 will be displayed once
尝试仅提供以下代码(不带消息)。可能是您提到的消息与警告不符。
导入警告
warnings.filterwarnings("ignore")
我想使用正则表达式抑制特定类型的警告。 警告信息是:
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.*"
不匹配 "\nA value is trying to be.*"
因为 r"."
匹配所有 除了换行符 .
有时不查看生成警告的模块的源代码就很难弄清楚实际的消息字符串是什么。
这不是 filterwarnings 的工作方式。在文档中您可以看到 Omitted arguments default to a value that matches everything.
,也可以看到:message (default '') : is a string containing a regular expression that start of the warning message must match
。
这可以理解为使用动作"once"会影响每条唯一的短信显示一次。如果您在消息中有一个可能会更改的字段(例如文件名),则警告将针对每个文件名显示一次。
如果您设置消息参数,每个匹配的唯一文本消息将显示一次。
这是一个小例子:
import warnings
import random
warnings.filterwarnings("ignore", category=UserWarning)
warnings.warn("You won't see this warning")
message_formater = "this message with number {} will be displayed once"
# deactivating previously created filter
warnings.resetwarnings()
# applying new filter
warnings.filterwarnings("once", message_formater.format("(.*)"), category=UserWarning)
for i in range(100):
warnings.warn(message_formater.format(random.randint(0, 3)))
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 0 will be displayed once
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 3 will be displayed once
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 1 will be displayed once
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 2 will be displayed once
尝试仅提供以下代码(不带消息)。可能是您提到的消息与警告不符。
导入警告 warnings.filterwarnings("ignore")