【问题标题】:Ignore warnings whose messages contain a specific string忽略消息包含特定字符串的警告
【发布时间】:2021-04-21 23:03:27
【问题描述】:

我不希望打印消息中包含“属性”的警告。我知道我可以通过指定整个消息来忽略警告:

import warnings

warnings.filterwarnings("ignore", message="All message displayed in console.")

我需要类似的东西:

warnings.filterwarnings("ignore", message="*property*")

我也知道我可以通过以下方式禁用代码特定部分的警告:

import warnings

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    function_that_causes_warnings()

【问题讨论】:

    标签: python python-3.x suppress-warnings


    【解决方案1】:

    filterwarningsmessage参数是正则表达式,应该可以使用

    warnings.filterwarnings("ignore", message=".*property.*")
    

    .* 匹配零次或多次出现的任何字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-30
      • 1970-01-01
      • 2015-10-10
      相关资源
      最近更新 更多