I have this function for replacing substrings according to a dict:
def unescape(val):
escaped_dict = {'7D5D':'7D', '7D33':'13', '7D31':'11', '7D5E':'7E'}
res = val
for key, value in escaped_dict.items():
res = res.replace(key, value)
return res
print(unescape("A20327D5D5D7D5D33"))
>>
A20327D5D13
ok changed 7D5D to 7D, but after it changed 7D33 to 13. I want to replace only once (eg: A20327D5D5D7D33).
The solution would be to add:
if key in val:
return res
But when there are several or more values from the dict to exchange it won't work.