Created
July 17, 2015 09:50
-
-
Save Hammer2900/e589e69a14628f83408a to your computer and use it in GitHub Desktop.
mass replace dict func
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment