I have a dictionary that may contain any arbitrary order of strings, lists of strings or nested dictionaries that ultimate terminate in strings. I would like to iterate over this dictionary and perform an action on each string.
This question is close to what I'm looking for but I was unsuccessful in applying that solution to my own.
I need to apply the function os.path.expanduser()
to each string in the following dictionary:
x = dict(
dir = dict(
wd = '~/Desktop/WD',
pymodule = [
'~/Documents/PythonModule',
'/Users/Username/Documents/PythonModule2'
],
album = '~/Desktop/Album'
),
file = dict(
XML = '~/Downloads/data.xml',
CSV = '~/Downloads/data.csv'
)
)
Ideally I would like to define a class that when called on a normal dictionary, will apply os.path.expanduser()
on each string element of that dictionary.
class MyDict:
def __init__(d):
self.d = d
# some operation to apply os.path.expanduser() on each string element of 'd'
How can I achieve this?