Reputation: 313
my dictionary:
mydict{'a':'/1/2/3/4/5/wrong1', 'b':'/x/y/x/u/t/wrong2'}
I would like parse the value, and replace 'wrong*' with 'right'. 'Right' is always the same, whereas 'wrong' is different each time.
'wrong' looks e.g. like that: folder_x/folder_y/somelongfilename.gz
'right' looks like that: *-AAA/debug.out
so afterwards my dictionary should look like that:
mydict{'a':'/1/2/3/4/5/right', 'b':'/x/y/x/u/right'}
Just replacing the value won't work here because I want to parse the value and replace only the last part of it. It is important to keep the first part of the value.
Does anyone have an idea how to solve this.
Thank you.
Upvotes: 0
Views: 541
Reputation: 118021
You could use a re.sub
to handle the replacement for you
>>> import re
>>> {k : re.sub('wrong', 'right', v) for k,v in mydict.items()}
{'b': '/x/y/x/u/t/right2',
'a': '/1/2/3/4/5/right1'}
Upvotes: 1