I am using Geopy to call different addresses, to see what cities and neighbourhoods they belong to.
from geopy.geocoders import GoogleV3
import json
places = '1238 Davie St, Vancouver, BC'
geolocator = GoogleV3(api_key='')
location = geolocator.geocode(places, language='en')
c = location.raw['address_components']
print c
counties = [c['long_name'] for c in c if 'neighborhood' in c['types']]
localities = [c['long_name'] for c in c if 'locality' in c['types']]
print counties, localities
Its working for counties, but not localities (cities).
[{u'long_name': u'1238', u'types': [u'street_number'], u'short_name': u'1238'}, {u'long_name': u'Davie Street', u'types': [u'route'], u'short_name': u'Davie St'}, {u'long_name': u'Central Vancouver', u'types': [u'neighborhood', u'political'], u'short_name': u'Central Vancouver'}, {u'long_name': u'Vancouver', u'types': [u'locality', u'political'], u'short_name': u'Vancouver'}, {u'long_name': u'Greater Vancouver', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'Greater Vancouver'}, {u'long_name': u'British Columbia', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'BC'}, {u'long_name': u'Canada', u'types': [u'country', u'political'], u'short_name': u'CA'}, {u'long_name': u'V6E 1N3', u'types': [u'postal_code'], u'short_name': u'V6E 1N3'}]
[u'Central Vancouver']
---> 32 localities = [c['long_name'] for c in c if 'locality' in c['types']]
33 print counties, localities
TypeError: string indices must be integers
Not sure why I am getting that mistake. I also want to be able to pass a Pandas column instead of typing in a static address, is there a neat way of doing that?