【发布时间】:2020-06-13 18:41:44
【问题描述】:
我正在解析亚马逊的报告表单,将行拆分为字段,然后创建 mysql 上传。我相信的数据最初是 iso-8859-1。除非数据中有一些特殊字符,如 Ä 或 ®,否则数据可以正常上传到 mysql。如果发生这种情况,我会收到类似pymysql.err.InternalError: (1366, "Incorrect string value: '\\xAE Kids...' for column 'item-name' at row 74") 和TypeError: can only concatenate str (not "bytearray") to str 的错误。我可以通过替换字节来解决它,但我不想建立一个巨大的列表,而且我真的想存储正确的值。我尝试更改我的 mysql 字符集和排序规则,但这似乎没有解决。我觉得修复是一个简单的修复,但我已经尝试了几个小时。
report_as_dict = report.parsed
report_as_dict = report_as_dict.replace(b' \r\n', b'\r\n') # remove black space at end
multi_line_rebuild=list()
for line in line_split[1:]:
field_split = line.split(b'\t')
logger.debug('Field Split : %s', field_split)
field_split = [x.replace(b'\x92', b'') for x in field_split] # removes single quotes
field_split = [x.replace(b'\xA0', b'') for x in field_split] # removes (
field_split = [x.replace(b'\xAE', b'') for x in field_split] # removes @
field_split = [x.replace(b'\xCD', b'l') for x in field_split] # replaces l with ' with l
field_split = [x.replace(b'\xE4', b'a') for x in field_split] # replaces a with two dots with a
multi_line_rebuild.append(field_split)
....
run_query_with_warnings(query_string, field_split=multi_line_rebuild)
功能
def run_query_with_warnings(warn_type, query_string, **kargs):
db = MySQLdb.connect(host=cred.host, user=cred.user, password=cred.password, db=cred.db, port=cred.port)
cursor = db.cursor()
cursor.executemany(query_string, kargs['field_split'])
【问题讨论】:
-
每张表都是utf8 / utf8_general_ci