【发布时间】:2018-11-05 23:43:35
【问题描述】:
我在网上到处寻找解决方案,但在任何地方都找不到。我正在从 CSV 文件中读取地址并进行地理编码并将两个新列写入新的 CSV。这适用于我选择的小样本量,但似乎我的 API 无法获取某些地址的经度和纬度值,从而导致错误。这是我的代码:
import geopy
import pandas
from geopy.geocoders import Bing
def main():
io = pandas.read_csv('newoutput3.csv',index_col=None, header=0,
sep=",",encoding='cp1252')
def get_latitude(x):
if x.latitude is None:
x.latitude = None
else:
return x.latitude
def get_longitude(x):
if x.longitude is None:
x.longitude = None
else:
return x.longitude
geolocator = Bing('myAPIkey',timeout=5)
geolocate_column = io['ADDRESS'].apply(geolocator.geocode)
io['latitude'] = geolocate_column.apply(get_latitude)
io['longitude'] = geolocate_column.apply(get_longitude)
io.to_csv('geocoding-output17.csv')
if __name__ == '__main__':
main()
File "C:/Users/Chris/Downloads/WPy-3662/scripts/geocoder.py", line 16, in
get_latitude
if x.latitude is None:
AttributeError: 'NoneType' object has no attribute 'latitude'
即使脚本无法获取某些地址的纬度或经度,我仍然希望脚本执行其功能,我该如何忽略它?
【问题讨论】:
-
问题是什么?如何不出现该错误?在这种情况下,您可能需要检查 x 是否为 None 而不是 x.latitude x.longitude 是否为 None。希望对您有所帮助。
-
请编辑您的帖子以包含错误的完整追溯的副本!
-
是的,我只是想知道如何忽略错误并继续执行脚本,我没有检查 x 是否为无吗?这就是我在我认为的两个功能中所做的。 @datapug
-
@seatal 在您检查的代码中是 x.latitude 是 None 但如果 x 是 None 则不是,它们是两个不同的东西。尝试用 x is None 和 x.longitude is None 替换 x.latitude is None 和 x is None
标签: python pandas csv geocode geopy