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

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

4

2 回答 2

2

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

from geopy.geocoders import Nominatim
geolocator = Nominatim()
place = 'Greece'
location = geolocator.geocode(place, language='en')
print location
'Greece'
于 2015-07-13T09:30:15.277 回答
2

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

print geolocator.geocode.func_code.co_varnames

'('self', 'query', 'exactly_one', 'timeout', 'addressdetails', 'language', 'geometry', 'params', 'url')'
于 2015-07-13T10:14:45.373 回答