Skip to content
Advertisement

Ignore warnings whose messages contain a specific string

I don’t want warnings whose message contains “property” to be printed. I know that I can ignore a warning by specifying its whole message with:

import warnings

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

I need something like:

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

I also know that I can disable warnings for a specific part of the code with:

import warnings

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

Advertisement

Answer

The message parameter of filterwarnings is a regular expression, therefore you should be able to use

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

Where .* matches zero or more occurrences of any character.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement