UnicodeEncodeError:“ascii”编解码器不能编码字符u'\xe9'

时间:2021-06-05 20:19:57

I use python to get json data from bing api

我使用python从bing api获取json数据

    accountKeyEnc = base64.b64encode(accountKey + ':' + accountKey)
    headers = {'Authorization': 'Basic ' + accountKeyEnc}
    req = urllib2.Request(bingUrl, headers = headers)
    response = urllib2.urlopen(req)
    content = response.read()
    data = json.loads(content)
    for i in range(0,6):
            print data["d"]["results"][i]["Description"]

But I got error

但我有错误

print data["d"]["results"][0]["Description"] UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 11: ordinal not in range(128)

打印数据["d"]["results"][0]["Description"] UnicodeEncodeError: 'ascii'编解码器不能将字符u'\xe9在位置11:序数不在范围内(128)

1 个解决方案

#1


0  

Your problem is that you are reading Unicode from the Bing API and then failing to explicitly convert it to ASCII. There does not exist a good mapping between the two. Prefix all of your const strings with u so that they will be seen as Unicode strings, see if that helps.

您的问题是您正在从Bing API读取Unicode,然后没有显式地将其转换为ASCII。两者之间不存在好的映射。在所有const字符串前面加上u前缀,以便它们被视为Unicode字符串,看看这是否有帮助。

#1


0  

Your problem is that you are reading Unicode from the Bing API and then failing to explicitly convert it to ASCII. There does not exist a good mapping between the two. Prefix all of your const strings with u so that they will be seen as Unicode strings, see if that helps.

您的问题是您正在从Bing API读取Unicode,然后没有显式地将其转换为ASCII。两者之间不存在好的映射。在所有const字符串前面加上u前缀,以便它们被视为Unicode字符串,看看这是否有帮助。