返回使用 geopy 和 try/except 时引发 AttributeError 的项目列表?
Returning a list of items that are raising an AttributeError when using geopy and try/except?
我正在尝试使用 geopy 获取约 400 个位置的纬度和经度。但是,许多地址是 returning 'None',因为它们要么不够具体,要么是街道的交叉路口,这导致了 "AttributeError: 'None' type has no attribute 'latitude'" 和 for 循环的退出。
我想做的是 运行 不因错误而退出的 for 循环和 return 所有提示错误的位置的列表,以便我可以硬编码正确的地址他们。
我目前的代码如下:
from geopy.geocoders import Nominatim
geolocator = Nominatim()
coordinates = []
def station_errors(list_of_stations):
for station in list_of_stations:
try:
location = geolocator.geocode(str(i)) #line with error, used 'i' instead of 'station'
lat_long = (location.latitude, location.longitude)
coordinates.append(list(lat_long))
except Exception:
print("Error: %s"%(station))
continue
但是,这似乎是在打印每个站点,而不管 geopy 是否能够为其分配经纬度,因为我已经手动尝试了很多。我只希望提出 error/were 的站点无法接收 lat/long 坐标。
我对 'try, except, continue' 错误测试还很陌生,所以非常感谢任何建议。
编辑:
我最初的错误是使用 'i' 而不是 'station',这是我在原始问题中留下的错误。我开始工作并给出我想要的结果的代码如下:
def station_errors(list_of_stations):
list_of_error_stations = []
for station in list_of_stations:
try:
location = geolocator.geocode(str(station + " DC"))
lat_long = (location.latitude, location.longitude)
coordinates.append(list(lat_long))
except Exception:
list_of_error_stations.append(station)
continue
return(list_of_error_stations)
我希望在行中给出“AttributeError: 'None' 类型没有属性 'latitude'”:
lat_long = (location.latitude, location.longitude)
因此,我会将代码转换为:
def station_errors(list_of_stations):
for station in list_of_stations:
location = geolocator.geocode(str(i))
coordinates.append([location.latitude, location.longitude] if location is not None else None)
# or coordinates.append(list(lat_long) if location is not None else [None, None])
# which ever comes more in handy
return coordinates
coords = station_errors(los)
# and then deal with the Nones (or [None, None] s ) to "hard code a proper address for them"
我正在尝试使用 geopy 获取约 400 个位置的纬度和经度。但是,许多地址是 returning 'None',因为它们要么不够具体,要么是街道的交叉路口,这导致了 "AttributeError: 'None' type has no attribute 'latitude'" 和 for 循环的退出。
我想做的是 运行 不因错误而退出的 for 循环和 return 所有提示错误的位置的列表,以便我可以硬编码正确的地址他们。
我目前的代码如下:
from geopy.geocoders import Nominatim
geolocator = Nominatim()
coordinates = []
def station_errors(list_of_stations):
for station in list_of_stations:
try:
location = geolocator.geocode(str(i)) #line with error, used 'i' instead of 'station'
lat_long = (location.latitude, location.longitude)
coordinates.append(list(lat_long))
except Exception:
print("Error: %s"%(station))
continue
但是,这似乎是在打印每个站点,而不管 geopy 是否能够为其分配经纬度,因为我已经手动尝试了很多。我只希望提出 error/were 的站点无法接收 lat/long 坐标。
我对 'try, except, continue' 错误测试还很陌生,所以非常感谢任何建议。
编辑:
我最初的错误是使用 'i' 而不是 'station',这是我在原始问题中留下的错误。我开始工作并给出我想要的结果的代码如下:
def station_errors(list_of_stations):
list_of_error_stations = []
for station in list_of_stations:
try:
location = geolocator.geocode(str(station + " DC"))
lat_long = (location.latitude, location.longitude)
coordinates.append(list(lat_long))
except Exception:
list_of_error_stations.append(station)
continue
return(list_of_error_stations)
我希望在行中给出“AttributeError: 'None' 类型没有属性 'latitude'”:
lat_long = (location.latitude, location.longitude)
因此,我会将代码转换为:
def station_errors(list_of_stations):
for station in list_of_stations:
location = geolocator.geocode(str(i))
coordinates.append([location.latitude, location.longitude] if location is not None else None)
# or coordinates.append(list(lat_long) if location is not None else [None, None])
# which ever comes more in handy
return coordinates
coords = station_errors(los)
# and then deal with the Nones (or [None, None] s ) to "hard code a proper address for them"