UnicodeEncodeError:'latin-1'编解码器无法在位置85处编码字符u'\ u05a0':序数不在范围(256)

时间:2019-04-24 10:50:30

标签: python json unicode iso-8859-1

我正在尝试在数据库中输入json格式的文件。我的json格式中有一些Unicode值。
有关Unicode错误的参考,请参见此链接 :Getting Unicode error while using insert statement

我通过使用编解码器Description for Codecs解决了 以及以上解决方案提供的解决方案。

但是现在当我执行插入语句时,标题出现错误。

我的Python代码如下:

import MySQLdb
import json



db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                 user="root",         # your username
                 passwd="password",  # your password
                 db="Mydb",)        # name of the data base


cursor = db.cursor()
json_file = open('asda.txt', 'r' )
file_data = json.load(json_file)
print(file_data)
print(type(file_data))

datas = file_data['datads']
print(datas)
for data in datas:
       print(data)
       print()
       print(data['first_col'])
       ex_statement = u"INSERT INTO `tablename` (`id`, `xv`) VALUES ( {id '"+unicode(data['first_col'])+ u"'}, {xv  '"+unicode(data['second_col'])+u"'});"
       #ex_statement = "Insert into `tablename` values {first_col '"+str(data['first_col'])+"'}, {second_col  '"+str(data['second_col'])+"'});"
       cursor.execute(ex_statement)
       cursor.commit()

db.close()

我的Json文件如下所示:

{"datads" :[{
     "first_col" : "SoomeVAlue_1",
     "second_col" : "SomeValue_1_1"
},
 {
     "first_col" : " Unicode_Start ֠  Unicode_End",
     "second_col" : "SomeValue_2_2"
},
{
     "first_col" : null ,
     "second_col" : "SomeValue_2_2"
}
]}

我的桌子看起来像: DataBase Image

执行文件后,我收到以下错误消息:

{u'datads': [{u'first_col': u'SoomeVAlue_1', u'second_col': u'SomeValue_1_1'}, {u'first_col': u' Unicode_Start \u05a0  Unicode_End', u'second_col': u'SomeValue_2_2'}, {u'first_col': None, u'second_col': u'SomeValue_2_2'}]}
<type 'dict'>
[{u'first_col': u'SoomeVAlue_1', u'second_col': u'SomeValue_1_1'}, {u'first_col': u' Unicode_Start \u05a0  Unicode_End', u'second_col': u'SomeValue_2_2'}, {u'first_col': None, u'second_col': u'SomeValue_2_2'}]
{u'first_col': u'SoomeVAlue_1', u'second_col': u'SomeValue_1_1'}
()
SoomeVAlue_1
{u'first_col': u' Unicode_Start \u05a0  Unicode_End', u'second_col': u'SomeValue_2_2'}
()
 Unicode_Start ֠  Unicode_End
Traceback (most recent call last):
  File "abc.py", line 35, in <module>
    cursor.execute(ex_statement)
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 188, in execute
    query = query.encode(db.encoding)




UnicodeEncodeError: 'latin-1' codec can't encode character u'\u05a0' in position 85: ordinal not in range(256)

请帮助我解决此错误。

1 个答案:

答案 0 :(得分:0)

该错误表明您的数据库连接使用Latin1(... / MySQLdb / cursors.py中的db.encoding'latin1')。 MySQLdb宣传它接受一个charset参数,因此您应该尝试:

db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                 user="root",         # your username
                 passwd="password",  # your password
                 db="Mydb",          # name of the data base
                 charset="utf-8",)   # charset for the connection

如果仍然不够,您可能必须切换到Python 3版本以及支持Python3的最新连接器(如mysql-connector-python)。由于Python3本机支持unicode,因此您应该不再有转换问题。