简体   繁体   中英

Suppress specific warning message

First, when doing an INSERT IGNORE , why does pymysql return a warning?

/python3.6/site-packages/pymysql/cursors.py:170: Warning: (1062, "Duplicate entry '2175891' for key 'PRIMARY'")

And second, is there a way to suppress these warnings? What I currently have is:

warnings.filterwarnings("ignore", category=pymysql.Warning)

Yet, I don't want to suppress all the pymysql warnings, only this one that seems a bit misplaced.

Use the message parameter. It accepts a regex:

warnings.filterwarnings(
  action="ignore", 
  message=".*Duplicate entry.*", 
  category=pymysql.Warning
)

message is a string containing a regular expression that the start of the warning message must match. The expression is compiled to always be case-insensitive.

Docs here :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM