我有一个调用谷歌API的功能:
def get_lat_long(place):
place = re.sub('\s','+', str(place), flags=re.UNICODE)
url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + place
content = urllib2.urlopen(url).read()
obj = json.loads(content)
results = obj['results']
lat = long = None
if len(results) > 0:
loc = results[0]['geometry']['location']
lat = float(loc['lat'])
long = float(loc['lng'])
return [lat, long]
但是,当我输入师大附中作为参数时,我收到错误:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
我尝试过str(地方).encode('utf-8'),但我认为这不是问题所在。我认为这是因为函数无法读取中文字符,所以在读取之前需要先将中文字符转换为unicode字符串吗?这只是一个猜测。
答案 0 :(得分:0)
假设place
属于unicode
类型,您需要执行以下操作:
def get_lat_long(place):
place = urllib.quote_plus(place.encode('utf-8'))
url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + place