I am trying to look up data in nested Python dictionaries by giving a path of keys.
For example, if my dict is
my_dict = {"key1": {"key2": 'my data'}}
I want to access my_dict['key1']['key2']
by giving the path 'key1|key2'
Here is what I tried:
all_keys = "key1|key2"
keys = all_keys.split("|")
key_path = ""
for key in keys:
key_path += '[\'' + key + '\']'
#my_dict is a dictionary with other dictionaries within it
returnval = my_dict + key_path
I am getting the error: TypeError: unsupported operand type(s) for +: 'dict' and 'str'