'Python: recursive function to find value in nested dictionary keeps returning 'None'
I have a nested dictionary containing microscopy acquisition metadata that would look like:
metadata_dict = {Metadata: {"_text" : "\n\n ",
"Core" : [{"_text" : \n\n ",
"Guid" : [{"_text" : "c..."}],
"UserID" : [{"_text" : "xyz"}]
"AppSW" : [{"_text" : "xT"}]
"AppSWVer" : [{"_text" : "0"}]
}],
"Instrument" : [{"_text" : "\n\n ",
"ContrSWVer : [{"_text": : "10..."}]
...
and so forth (hope the indentations make it a bit less chaotic). I only require certain key/value pairs and I'm using python to get the values to specifiable keys. The .get() method returns 'None' no matter the argument, so I tried the following to go over all values in the dictionary to find specific keys:
def lookup(dic,prop):
for k, v in dic.items():
if k == prop:
if not isinstance(v, dict):
return v
else:
for key, value in v:
return value
elif isinstance(v, dict):
lookup(v, prop)
Calling this method, e. g. by
UserID = lookup(metadata_dict, 'UserID')
print(UserID)
I only get 'None' outputs regardless of the level at which the argument is nested in the dictionary.
This post seemed to tackle the same problem, however, after changing the last two lines to
elif isinstance(v, dict):
return lookup(v, prop)
I still get but 'None' returns. Could this be caused by recursively calling the method with the second argument being prop resulting in the method running with v, prop (and prop being an undefined variable) rather than inheriting the argument ('UserID' in the example) from the initial function? If so, why wouldn't this return an error, considering prop is undefined?
Solution 1:[1]
You can try this to see if it works, if not, i suggest that you explain the results you want, you can use data as an example
def lookup(dic, prop):
res = []
for item in dic.values():
if isinstance(item, list):
for sub_item in item:
for k, v in sub_item.items():
if k == prop:
res.append(v)
elif isinstance(item, dict):
for k, v in item.items():
if k == prop:
res.append(v)
return res
metadata_dict = {"Metadata": {"_text" : "\n\n ",
"Core" : [{"_text" : "\n\n ",
"Guid" : [{"_text" : "c..."}],
"UserID" : [{"_text" : "xyz"}],
"AppSW" : [{"_text" : "xT"}],
"AppSWVer" : [{"_text" : "0"}]
}],
"Instrument" : [{"_text" : "\n\n ",
"Guid" : [{"_text" : "124"}],
"UserID" : [{"_text" : "waefe"}],
"AppSW" : [{"_text" : "asfa"}],
"AppSWVer" : [{"_text" : "21"}]
}]
}}
print(lookup(metadata_dict["Metadata"], "UserID"))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | maya |
