Python地理代码把我的城市名字改成了奇怪的名字

2024-04-29 11:17:16 发布

您现在位置:Python中文网/ 问答频道 /正文

https://pypi.python.org/pypi/geopy的指导下,我尝试获取几个城市的lat/lon信息;但是,它并不像预期的那样对我有效。当我输入城市名称列表时,位置会根据我的城市创建一些有线名称,但其他城市的名称正常工作。然后它抱怨UnicodeEncodeError:'ascii'编解码器无法对位置5中的字符u'\xed'进行编码:序号不在范围内(128)。具体代码如下:

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

输出结果如下:所以当我第一次打印城市名称(df['google']时,您可以看到,这是正常的;当我将其转换为list时,它仍然是正常的;但是,在geocode函数之后,位置看起来是有线的。有人能帮忙吗?非常感谢!你知道吗

 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

Tags: in名称pypidfgooglelocationgeocodeprint