I am using the following code to normalize a file's name:
new_file = re.sub('[. ]', '_', old_file.lower())
new_file = re.sub('__+', '_', new_file)
new_file = re.sub('[][)(}{]', '', new_file)
new_file = re.sub('[-_]([^-_]+)$', r'.\1', new_file)
My question is there a possibility to write this code in a better way?
I found the following example:
def multiple_replace(dict, text):
# Create a regular expression from the dictionary keys
regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
# For each match, look-up corresponding value in dictionary
return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
dict = {
"Larry Wall" : "Guido van Rossum",
"creator" : "Benevolent Dictator for Life",
"Perl" : "Python",
}
But this code only works with normal strings. The map(re.escape, ...
in line 3 "destroys" the Regex.
Regards,
Ray