Python:Unicode和

2024-05-16 01:09:32 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: key代码inimporteventapijsoncity
1条回答
网友
1楼 · 发布于 2024-05-16 01:09:32

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

相关问题 更多 >