Python:Unicode 和 Str

Python: Unicode and Str

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

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."))

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