简体   繁体   中英

Python: Searching a multi-dimensional dictionary for a key

I'm using Python's JSON decoding library with Google Maps API. I am trying to obtain the zip code of an address but it sometimes resides in different dictionary key. Here are two examples (I've trimmed the JSON to what is relevant):

placemark1 = {
  "AddressDetails": {
    "Country": {
      "AdministrativeArea": {
        "SubAdministrativeArea": {
          "Locality": {
            "PostalCode": {
              "PostalCodeNumber": "94043"
            }
          }
        }
      }
    }
  }
}

( View full JSON )

placemark2 = {
  "AddressDetails": {
    "Country" : {
      "AdministrativeArea" : {
        "Locality" : {
          "PostalCode" : {
            "PostalCodeNumber" : "11201"
          }
        }
      }
    }
  }
}

( View full JSON )

So the zipcodes:

zipcode1 = placemark1['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['PostalCode']['PostalCodeNumber']
zipcode2 = placemark2['AddressDetails']['Country']['AdministrativeArea']['Locality']['PostalCode']['PostalCodeNumber']

Now I was thinking perhaps I should just search the multi-dimensional dictionary for "PostalCodeNumber" key. Does anyone have any idea on how to accomplish this? I want it to look something like this:

>>> just_being_a_dict = {}
>>> just_a_list = []
>>> counter_dict = {'Name': 'I like messing things up'}
>>> get_key('PostalCodeNumber', placemark1)
"94043"
>>> get_key('PostalCodeNumber', placemark2)
"11201"
>>> for x in (just_being_a_dict, just_a_list, counter_dict):
...     get_key('PostalCodeNumber', x) is None
True
True
True
def get_key(key,dct):
    if key in dct:
        return dct[key]
    for k in dct:
        try:
            return get_key(key,dct[k])
        except (TypeError,ValueError):
            pass
    else:
        raise ValueError

placemark1 = {
  "AddressDetails": {
    "Country": {
      "AdministrativeArea": {
        "SubAdministrativeArea": {
          "Locality": {
            "PostalCode": {
              "PostalCodeNumber": "94043"
            }
          }
        }
      }
    }
  }
}

placemark2 = {
  "AddressDetails": {
    "Country" : {
      "AdministrativeArea" : {
        "Locality" : {
          "PostalCode" : {
            "PostalCodeNumber" : "11201"
          }
        }
      }
    }
  }
}

just_being_a_dict = {}
just_a_list = []
counter_dict = {'Name': 'I like messing things up'}

for x in (placemark1, placemark2, just_being_a_dict, just_a_list, counter_dict):
    try:
        print(get_key('PostalCodeNumber', x))
    except ValueError:
        print(None)

yields

94043
11201
None
None
None
from collections import Mapping

zipcode1 = {'placemark1':{'AddressDetails':{'Country':{'AdministrativeArea':{'SubAdministrativeArea':{'Locality':{'PostalCode':{'PostalCodeNumber':"94043"}}}}}}}}
zipcode2 = {'placemark2':{'AddressDetails':{'Country':{'AdministrativeArea':{'Locality':{'PostalCode':{'PostalCodeNumber':'11201'}}}}}}}

def treeGet(d, name):
    if isinstance(d, Mapping):
        if name in d:
            yield d[name]
        for it in d.values():
            for found in treeGet(it, name):
                yield found

yields all matching values in tree:

>>> list(treeGet(zipcode1, 'PostalCodeNumber'))
['94043']
>>> list(treeGet(zipcode2, 'PostalCodeNumber'))
['11201']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM