Christian
Christian

Reputation: 21

Python: Google-Maps-API sends unknown format to parse

I use the Python Client for Google Maps Services to get following data from google-maps:

{  
   'address_components':[  
      {  
         'long_name':'20',
         'short_name':'20',
         'types':[  
            'street_number'
         ]
      },
      {  
         'long_name':'Oberböhl',
         'short_name':'Oberböhl',
         'types':[  
            'route'
         ]
      },
      {  
         'long_name':'Ingelheim am Rhein',
         'short_name':'Ingelheim am Rhein',
         'types':[  
            'locality',
            'political'
         ]
      },
      {  
         'long_name':'Mainz-Bingen',
         'short_name':'Mainz-Bingen',
         'types':[  
            'administrative_area_level_3',
            'political'
         ]
      },
      {  
         'long_name':'Rheinland-Pfalz',
         'short_name':'RP',
         'types':[  
            'administrative_area_level_1',
            'political'
         ]
      },
      {  
         'long_name':'Germany',
         'short_name':'DE',
         'types':[  
            'country',
            'political'
         ]
      },
      {  
         'long_name':'55218',
         'short_name':'55218',
         'types':[  
            'postal_code'
         ]
      }
   ],
   'adr_address':'<span class="street-address">Oberböhl 20</span>, <span class="postal-code">55218</span> <span class="locality">Ingelheim am Rhein</span>, <span class="country-name">Germany</span>',
   'formatted_address':'Oberböhl 20, 55218 Ingelheim am Rhein, Germany',
   'formatted_phone_number':'06132 5099968',
   'geometry':{  
      'location':{  
         'lat':49.9810156,
         'lng':8.0739617
      },
      'viewport':{  
         'northeast':{  
            'lat':49.9823942302915,
            'lng':8.075293780291501
         },
         'southwest':{  
            'lat':49.9796962697085,
            'lng':8.072595819708498
         }
      }
   },
   'icon':'https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png',
   'id':'d2b37ffe23fd5e76648a90df2987558b039fcdf7',
   'international_phone_number':'+49 6132 5099968',
   'name':'Esch Metalltechnik GmbH',
   'place_id':'ChIJHaERGJ_svUcRRfqNoGXq3EU',
   'plus_code':{  
      'compound_code':'X3JF+CH Ingelheim am Rhein, Germany',
      'global_code':'8FXCX3JF+CH'
   },
   'reference':'ChIJHaERGJ_svUcRRfqNoGXq3EU',
   'scope':'GOOGLE',
   'types':[  
      'general_contractor',
      'point_of_interest',
      'establishment'
   ],
   'url':'https://maps.google.com/?cid=5034156205699627589',
   'utc_offset':60,
   'vicinity':'Oberböhl 20, Ingelheim am Rhein',
   'website':'http://www.esch-metalltechnik.de/'
}{  
   'long_name':'55218',
   'short_name':'55218',
   'types':[  
      'postal_code'
   ]
}

Now I want to extract certain variables, like the "street_number". I don't know which format this data is, so I worked with it like a dictionary:

try:
    self.hausnr = place_result_2["address_components"][0]["long_name"]
except:
    self.hausnr = "NA"

The problem is, that the index "0" isn't always the same position of the data I want, i varies. Is there a way to extract the data in another way? Perhaps I have to use a JSON-parser or something similar?

Thanks a lot.

Upvotes: 2

Views: 184

Answers (1)

Tomalak
Tomalak

Reputation: 338336

The answer is: List comprehensions

try:
    # make a list of all address components that have type "street number"
    comp = [c for c in place_result_2["address_components"] if "street_number" in c["types"]]

    # the first one of them (assuming there will never be more than one) is the desired one
    self.hausnr = comp[0]["long_name"]
except:
    self.hausnr = "NA"

Since this will probably be a common operation, make a function:

def get_address_component(place_result, comp_type, comp_property="long_name", default=None):
    """ returns the first address component of a given type """
    try:
        comp = [c for c in place_result["address_components"] if comp_type in c["types"]]
        return comp[0][comp_property]
    except KeyError:
        return default

# ...

self.hausnr = get_address_component(place_result_2, "street_number", default="NA")

PS, regarding:

Perhaps I have to use a JSON-parser or something similar?

JSON is a data transfer format - it's plain text. The Google API server used it to get the data across the wire. In your program it has already been parsed - by the Google API client library you are using. What you are looking at is not JSON anymore, it's a Python data structure (nested dicts and lists and values). It just happens to look quite similar to JSON when you print it to the console, because Python uses a similar format to represent data.

On other words, no, you don't need to JSON-parse it again.

Upvotes: 1

Related Questions