Search code examples
pythongeopy

Geopy chokes on ASCII characters


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

from geopy.geocoders import Nominatim
geolocator = Nominatim()

place = 'Greece'

location = geolocator.geocode(place)
print location

Error:

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)

Second try with decoding:

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

from geopy.geocoders import Nominatim
geolocator = Nominatim()

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

location = geolocator.geocode(place)
print location

Error:

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)

Check type of place_decoded:

print type(place_decoded)
<type 'unicode'>

The word Greece should not give any trouble. Anyone having an idea?


Solution

  • i found that the easy solution is working with english :)

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