[英]UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9'
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”] [“结果”] [0] [“描述”] UnicodeEncodeError:'ascii'编解码器无法在位置11编码字符u'\\ xe9':序数不在范围内(128)
Your problem is that you are reading Unicode from the Bing API and then failing to explicitly convert it to ASCII. 您的问题是您正在从Bing API中读取Unicode,然后无法将其显式转换为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. 用
u
前缀所有const字符串,以便将它们视为Unicode字符串,看看是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.