ASCII字符上的Geopy chokes

时间:2015-07-13 08:54:29

标签: python geopy

# -*- coding: utf-8 -*-

from geopy.geocoders import Nominatim
geolocator = Nominatim()

place = 'Greece'

location = geolocator.geocode(place)
print location

错误:

Traceback (most recent call last):
File "C:/temp/Test.py", line 10, in <module>
print location
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

第二次尝试解码:

# -*- coding: utf-8 -*-

from geopy.geocoders import Nominatim
geolocator = Nominatim()

place = 'Greece'
place_decoded = place.decode('utf-8')

location = geolocator.geocode(place)
print location

错误:

Traceback (most recent call last):
File "C:/temp/Test.py", line 10, in <module>
print location
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

检查place_decoded的类型:

print type(place_decoded)
<type 'unicode'>

希腊这个词不应该给任何麻烦。有想法的人吗?

2 个答案:

答案 0 :(得分:2)

我发现简单的解决方案是使用英语:)

from geopy.geocoders import Nominatim
geolocator = Nominatim()
place = 'Greece'
location = geolocator.geocode(place, language='en')
print location
'Greece'

答案 1 :(得分:2)

只是让你知道,如果你想知道函数可以采取什么参数,你可以输入:

print geolocator.geocode.func_code.co_varnames

'('self', 'query', 'exactly_one', 'timeout', 'addressdetails', 'language', 'geometry', 'params', 'url')'