I've looked everywhere online trying to find a solution but I can't find one anywhere. I'm reading an address from a CSV file and geocoding and writing two new columns into a new CSV. This works with small sample sizes I've chosen, but it appears my API can't get the longitude and latitude values for some addresses thus causing an error. Here's my code:
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'
I still want the script to do what it does even if it can't get the latitude or longitude for certain addresses, how do i go about ignoring it?