Python:Unicode和Str

时间:2015-01-21 17:07:14

标签: python json unicode

str转换为unicode的最佳方式是什么?当我运行代码时,我收到以下错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 13: ordinal not in range(128).

我的代码:

import json
import urllib2

def locu_data(city,api_key):
    local_business_data='https://api.locu.com/v1_0/venue/search/?locality='+city+'&api_key='+api_key
    open_local_business_data=urllib2.urlopen(local_business_data)
    data_load=json.load(open_local_business_data)
    Business = [x for x in data_load['objects']]
    Event=[]
    for item in Business:
        if (item != None):
            Event.append(('Name:{},Categories:{},City:{},Longitutde:{},Latitude:{},Website:{}\n').format(item['name'],item['categories'],item['locality'],item['long'],item['lat'],item['website_url']))
        else:
            Event.append('None')
    print Event

locu_data(city=raw_input("Please enter the city you would like to analyze:"),api_key=raw_input("Please enter your locu API key."))

1 个答案:

答案 0 :(得分:0)

只需使用string.decode(),其中string是您正在操作的变量。