Geopy 在 ASCII 字符上阻塞

Geopy chokes on ASCII characters

# -*- 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'>

希腊这个词应该不会造成任何麻烦。有人有想法吗?

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

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

只是想让你知道,如果你想知道函数可以接受哪些参数,你可以输入:

print geolocator.geocode.func_code.co_varnames

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