I try to get the lat/lon info for several cities following the guidance of https://pypi.python.org/pypi/geopy; however, it does not work for me as expected. When I input the list of city names, the location creates some wired names based on my cities but work normal for others. Then it complains UnicodeEncodeError: 'ascii' codec can't encode character u'\xed' in position 5: ordinal not in range(128). the exact code is as below:
from geopy.geocoders import Nominatim
geolocator = Nominatim()
df=pd.read_csv('testgoo0122.csv')
print df['google']
list1=df['google'].tolist()
print list1
for i in range(0,len(list1)):
location = geolocator.geocode("'"+list1[i]+"'")
print location
#I also tried, which has the exact same problem.
for i in range(0,len(df.google)):
location=geolocator.geocode("'"+df.google[i]+"'")
print location
The output looks like: so you can see when i first print city names (df['google'], it is normal; when i convert it to list, it is still normal; however, after the geocode function, the location looks wired. Can anyone help? many thanks!
0 Kerugoya,Kirinyaga,KENYA
1 Mie,JAPAN
2 Hhohho,SWAZILAND
3 Granma,CUBA
4 Holguin,CUBA
Name: google, dtype: object
['Kerugoya,Kirinyaga,KENYA', 'Mie,JAPAN', 'Hhohho,SWAZILAND', 'Granma,CUBA', 'Holguin,CUBA']
Kerugoya, Kirinyaga, Kenya
Japan, Luzerne County, Pennsylvania, 18221, United States of America
Hhohho, eSwatini Swaziland
Granma, Cuba
Traceback (most recent call last):
File "testgoogle2.py", line 30, in <module>
print location