首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnicodeEncodeError:'ascii‘编解码器不能编码字符u'\u05a0’,位置34:序数不在范围(128)

UnicodeEncodeError:'ascii‘编解码器不能编码字符u'\u05a0’,位置34:序数不在范围(128)
EN

Stack Overflow用户
提问于 2019-04-22 09:52:01
回答 2查看 375关注 0票数 0

我试图在python中运行一段代码,在那里我必须读取一个包含Json格式代码的filename.txt文件。但我在json值中有一些Unicode值。这个文件非常大,但是我在文件中找到了一个Unicode作为֠,这个符号的unicode是u"\u05A0"

有关unicode Unicode字符‘希伯来口音TELISHA GEDOLA’(U+05A0)的更多信息,可以参考此链接。

我的Python代码看起来像

代码语言:javascript
复制
import MySQLdb
import json



db = MySQLdb.connect(host="10.233.188.84",    # your host, usually localhost
                 user="root",         # your username
                 passwd="freebird@123",  # your password
                 db="Practice_For_Json",)        # 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:
    ex_statement = "Insert into `tablename` values {first_col '"+str(data['first_col'])+"'}, {second_col  '"+str(data['second_col'])+"'});"
#    cursor.execute(ex_statement)


db.close()

我的Json看起来:

代码语言:javascript
复制
{"datads" :[{
      "first_col" : "SoomeVAlue_1",
      "second_col" : "SomeValue_1_1"
},
{
     "first_col" : " Unicode_Start ֠  Unicode_End",
     "second_col" : "SomeValue_2_2"
}]}

上述代码的输出如下:

代码语言:javascript
复制
{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'}]}
<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'}]
Traceback (most recent call last):
  File "abc.py", line 21, in <module>
    ex_statement = "Insert into `tablename` values {first_col '"+str(data['first_col'])+"'}, {second_col  '"+str(data['second_col'])+"'});"




UnicodeEncodeError: 'ascii' codec can't encode character u'\u05a0' in position 15: ordinal not in range(128)

当我运行这段代码时,我会得到错误作为标题。

我在SSH shell中使用Python2.7。

请帮我处理这个。

EN

Stack Overflow用户

发布于 2019-04-22 10:10:16

我想你可以试试这个

代码语言:javascript
复制
json_file = open('test.txt','rb')
json_file = json_file.read().decode('UTF-8')
票数 -1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55792483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档