I am trying to get the names of counties for a list of addresses. To do that, I am passing addresses to Google geocoding API using geopy and using raw method. The issue is parsing through the dictionary to get that chunk of data.
I need the part that is in administrative_area_level_2, but this is a list now. I can't parse by the sequence of items in the list because all the addresses are formatted differently. What would be the best way to parse through this?
import json
places = ['901 Main St Dallas, TX']
geolocator = GoogleV3(api_key='')
location = geolocator.geocode(places, language='en')
print location.raw['address_components']```
This is the result that I am getting:
[{u'long_name': u'Bank of America Plaza', u'types': [u'premise'], u'short_name': u'Bank of America Plaza'}, {u'long_name': u'901', u'types': [u'street_number'], u'short_name': u'901'}, {u'long_name': u'Main Street', u'types': [u'route'], u'short_name': u'Main St'}, {u'long_name': u'Downtown', u'types': [u'neighborhood', u'political'], u'short_name': u'Downtown'}, {u'long_name': u'Dallas', u'types': [u'locality', u'political'], u'short_name': u'Dallas'}, {u'long_name': u'Dallas County', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'Dallas County'}, {u'long_name': u'Texas', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'TX'}, {u'long_name': u'United States', u'types': [u'country', u'political'], u'short_name': u'US'}, {u'long_name': u'75202', u'types': [u'postal_code'], u'short_name': u'75202'}]