Python 初学者,我在写一个基础类,有个导入文件进 MySQL 的方法,导入时不免会有一些 warning ,我想把 warning 捕获。然后返回给上层业务代码。
xxx.py:247: Warning: Data truncated for column 'cost' at row 2004
cursor.execute(load_data_sql)
xxx.py:247: Warning: Row 2004 was truncated; it contained more data than there were input columns
cursor.execute(load_data_sql)
xxx.py:247: Warning: Data truncated for column 'cost' at row 3519
cursor.execute(load_data_sql)
xxx.py:247: Warning: Row 3519 was truncated; it contained more data than there were input columns
cursor.execute(load_data_sql)
xxx.py:247: Warning: Data truncated for column 'cost' at row 5280
cursor.execute(load_data_sql)
xxx.py:247: Warning: Row 5280 was truncated; it contained more data than there were input columns
cursor.execute(load_data_sql)
xxx.py:247: Warning: Data truncated for column 'cost' at row 7034
cursor.execute(load_data_sql)
xxx.py:247: Warning: Row 7034 was truncated; it contained more data than there were input columns
cursor.execute(load_data_sql)
xxx.py:247: Warning: Data truncated for column 'cost' at row 8801
cursor.execute(load_data_sql)
xxx.py:247: Warning: Row 8801 was truncated; it contained more data than there were input columns
cursor.execute(load_data_sql)
……
我尝试了一下:
cursor = db.cursor()
load_data_sql = "LOAD DATA INFILE \'%s\' IGNORE INTO TABLE %s" \
% (cleaned_file_path, table)
# 捕获 warn
warnings.filterwarnings('error')
warn = ''
try:
cursor.execute(load_data_sql)
db.commit()
except MySQLdb.Error as e:
db.rollback()
return {
'status': -1,
'message': "load data file sql \"%s\" error: %s" % (load_data_sql, e)
}
except MySQLdb.Warning as e:
return {
'status': -2,
'message': "load data file sql \"%s\" warning: %s" % (load_data_sql, e)
}
else:
return {
'status': 0,
'message': 'success'
}
finally:
db.close()
上层业务会打日志:
warning: Data truncated for column 'cost' at row 2004
貌似还行,除了忽略的行,其它数据都导入成功了。但总感觉代码有点 hack ( filterwarnings ),而且只能捕获到一条 Warning ,应该会有很多条的。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.