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?