1
votes

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)

1
@Elizion sorry, I did not get it, how to use... - Sihan Wang
What is accountKey? - HavelTheGreat
@Elizion bing's account - Sihan Wang
That error indicates that Python does not know what encoding to use for console output - it's assuming the lowest common denominator, which is 'ascii'. There are lots of characters that just don't exist in ASCII so you get an error when you try to print them. - Mark Ransom

1 Answers

0
votes

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.