简体   繁体   中英

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9'

I use python to get json data from bing api

    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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM