'latin-1'编解码器无法在位置23编码字符u'\ u2014':序数不在范围内(256) [英] 'latin-1' codec can't encode character u'\u2014' in position 23: ordinal not in range(256)
问题描述
我正在将数据从excel工作簿加载到pandas数据框中,并在遇到上述错误时尝试将其推送到数据库中.
I am loading data into a pandas dataframe from an excel workbook and am attempting to push it to a database when I get the above error.
我最初以为数据库的整理存在争议,我将其更改为utf8_bin
I thought at first the collation of the database was at issue which I changed to utf8_bin
接下来,我检查了数据库引擎create语句,该语句也为编码添加了一个参数.
Next I checked the database engine create statement on my end which I added a parameter for the encoding too.
engine = create_engine('mysql+pymysql://root@localhost/test', encoding="utf-8")
但是这些东西都不起作用,我仍然从该行中得到错误:
But neither of these things work I am still getting the error from the line:
df.to_sql("strand", engine, if_exists="append", index=False)
我检查了to_sql方法是否有编码参数,但事实并非如此.
I checked if there was an encoding parameter for the to_sql method but this does not seem to be the case.
推荐答案
显然,我需要在查询字符串和编码变量中添加?charset-utf8,这导致我最终在引擎create语句中结束
Apparently I needed to add ?charset-utf8 to the query string as well as the encoding variable which resulted in me ending up wht the engine create statement
engine = create_engine('mysql+pymysql://root@localhost/test?charset=utf8', encoding="utf-8")
这篇关于'latin-1'编解码器无法在位置23编码字符u'\ u2014':序数不在范围内(256)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!