抑制特定警告消息

2024-03-28 10:04:38 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,在执行INSERT IGNORE时,为什么pymysql会返回警告

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

第二,有没有办法抑制这些警告?我目前拥有的是:

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

然而,我不想抑制所有的pymysql警告,只有这一个似乎有点放错地方了


Tags: keypy警告forpackagessiteinsertignore
1条回答
网友
1楼 · 发布于 2024-03-28 10:04:38

使用message参数。它接受正则表达式:

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

相关问题 更多 >