-1

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'}]
user4718221
  • 561
  • 6
  • 20
  • possible duplicate of [How to extract a specific data from the json using python](https://stackoverflow.com/questions/37865043/how-to-extract-a-specific-data-from-the-json-using-python) – geocodezip Apr 12 '19 at 03:33

1 Answers1

1

You can parse the list like this:

l = []
for i in c:
  for k, v in i.items():
    if k == 'types' and 'administrative_area_level_2' in v:
      l.append(i['long_name'])
print(l)
Arun Augustine
  • 1,690
  • 1
  • 13
  • 20