If I have the following dictionary:
foo = {'bar': {'baz': {'qux': 'gap'} } }
I want the user to be able to input "'bar','baz','qux','dop'" [expansion: " 'bar' , 'baz ' , 'qux' , 'dop' "] to convert:
{'qux': 'gap'}
to
{'qux': 'dop'}
I was hoping to approach this by converting the user input to a dictionary-lookup-statement (unsure of the exact term) through the following:
objectPath = "foo"
objectPathList = commandList[:-1] # commandList is the user input converted to a list
for i in objectPathList:
objectPath += "[" + i + "]"
changeTo = commandList[-1]
The above makes objectPath = "foo['bar']['baz']['qux']" and changeTo = 'dop'
Great! However, now I have been having issues with turning that statement into code. I thought eval() would do the trick, however the following seems not to work:
eval(objectPath) = changeTo
How can I convert the string objectPath to replace hard-written code?
eval(objectPath + "='" + changeTo "'")