espnetez.config.convert_none_to_None
Less than 1 minute
espnetez.config.convert_none_to_None
espnetez.config.convert_none_to_None(dic)
Recursively convert string representations of ‘none’ in a dictionary to None.
This function traverses a dictionary and replaces any occurrences of the string “none” with the actual Python None type. If a value in the dictionary is another dictionary, this function is called recursively to ensure all nested dictionaries are processed.
- Parameters:dic (dict) – A dictionary potentially containing the string “none” as a value.
- Returns: The input dictionary with all instances of the string “none” replaced by None.
- Return type: dict
Examples
>>> sample_dict = {'key1': 'none', 'key2': {'subkey1': 'none',
'subkey2': 'value'}}
>>> convert_none_to_None(sample_dict)
{'key1': None, 'key2': {'subkey1': None, 'subkey2': 'value'}}
>>> nested_dict = {'level1': {'level2': {'level3': 'none'}}}
>>> convert_none_to_None(nested_dict)
{'level1': {'level2': {'level3': None}}}
NOTE
This function modifies the input dictionary in place, but it also returns the modified dictionary for convenience.