I have a very long string, that looks like a dictionary, but where many of the keys have been replaced by variables that are undefined, e.g.:
dict_1_str = '{a: "date", b: 7, c: {d: 5, e: "Hello There!"}}'
In an actual dictionary, I have the variable names and values.
dict_2 = {"a": 1, "b": 2, "c": 3, "d": "General Kenobi!", "e": 5}
I want to update the keys in the top dictionary with the values in the bottom dictionary. I thought I could create a list of dict_2
's keys and a list of dict_2
's values, and then use exec()
to evaluate the lists as equal, and so set all the variables in one go — but I cannot get it to work, and I would rather avoid exec()
if I can.
I tried to use regex, but many of string values in dict_1_str
contain the names of the keys/variables. Is there some elegant way of doing this?