ibrahem
ibrahem

Reputation: 530

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()

Upvotes: 5

Views: 2729

Answers (1)

mkrieger1
mkrieger1

Reputation: 23255

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.

Upvotes: 12

Related Questions