【发布时间】:2016-06-03 07:47:50
【问题描述】:
我一直在导入一个包含多个地址的 csv 数据集。我想获取这些地方的纬度和经度,并将它们与原始地址一起写入一个新的 csv 文件。我一直在尝试使用 python 中的 Geopy 来实现这一点。代码如下:
import csv
##from time import sleep
from geopy.geocoders import Nominatim
with open('D:/location_to_lat_lon/tolocate.csv', 'r') as fp:
with open('D:/location_to_lat_lon/places_located.csv', 'w',newline='') as op:
a = csv.writer(op)
a.writerow(["Town","District","State","Country","Address","Latitude","Longitude"])
for line in fp.readlines():
geolocator = Nominatim()
town_new = line.split(',')[0]
district_new = line.split(',')[1]
state_new = line.split(',')[2]
country_new = line.split(',')[3]
address_new = line.split(',')[4]
location = geolocator.geocode(address_new)
lat=location.latitude
lon=location.longitude
##time.sleep(3)
a.writerow([town_new,district_new,state_new,country_new,address_new,lat,lon])
但是,每次我运行此代码时,都会出现以下错误
Traceback(最近一次调用最后一次):文件“”,第 13 行,in lat=location.latitude AttributeError: 'NoneType' 对象没有属性 'latitude
谁能帮我解决这个问题?
'
【问题讨论】:
-
你没有考虑
location = geolocator.geocode(address_new)返回None时的情况 -
如何在我的代码中处理它?