So I have a nested dictionary in Python:
{'entry': {'definition': 'str',
'endTime': 'str',
'entryID': {'identifierType': 'str', 'identifierValue': 'str'},
'instrument': {'FIB': {'FIBSpotSize': {'notes': 'str',
'qualifier': 'str',
'uncertainty': {'uncertaintyType': 'str',
'value': 'float'},
'unit': 'str',
'value': 'float'},
'angleToEBeam': {'notes': 'str',
'qualifier': 'str',
'uncertainty': {'uncertaintyType': 'str',
'value': 'float'},
'unit': 'str',
'value': 'float'},...
.
.
.
I want a way to simply access a specific key-value pair, given an arbitrarily long keys.
For instance, I want entry.instrument.angleToEBeam.uncertainty.value
because I want to update the value of value
. I want to write a function that takes a list of keys of arbitrary length, where the last key is the value I'd like to update, and the value which I'd like to set/update. I'm a bit confused on how to navigate/walk these nested dictionaries.
As an example of what I'm trying to do, I have a dict of values to update which looks like
{'entry.instrument.angleToEBeam.uncertainty.value': 2.23', ...}
And I'd like to feed this list into some function such that it updates the nested dict to have values instead of the expected type, here shown just for the uncertainty
parameter:
{'entry': {'definition': 'str',
'endTime': 'str',
'entryID': {'identifierType': 'str', 'identifierValue': 'str'},
'instrument': {'FIB': {'FIBSpotSize': {'notes': 'str',
'qualifier': 'str',
'uncertainty': {'uncertaintyType': 'str',
'value': 'float'},
'unit': 'str',
'value': 'float'},
'angleToEBeam': {'notes': 'str',
'qualifier': 'str',
'uncertainty': {'uncertaintyType': 'pct',
'value': 3.102},
'unit': 'deg',
'value': 2.23},...
.
.
.
Ultimately, these have to be exported as JSON files, hence the nested dictionary. Any ideas? Some related threads:
- Update value of a nested dictionary of varying depth
- How to completely traverse a complex dictionary of unknown depth?
But I couldn't figure out how to apply them to my situation, as they deal with flattening the dict entirely.