Here is a short example that should do the trick with regular expressions:
import re
rep = {"condition1": "", "condition2": "text"} # define desired replacements here
# use these three lines to do the replacement
rep = dict((re.escape(k), v) for k, v in rep.items())
pattern = re.compile("|".join(rep.keys()))
text = pattern.sub(lambda m: rep[re.escape(m.group(0))], text)
For example:
>>> pattern.sub(lambda m: rep[re.escape(m.group(0))], "(condition1) and --condition2--")
'() and --text--'
Answer from Andrew Clark on Stack OverflowHere is a short example that should do the trick with regular expressions:
import re
rep = {"condition1": "", "condition2": "text"} # define desired replacements here
# use these three lines to do the replacement
rep = dict((re.escape(k), v) for k, v in rep.items())
pattern = re.compile("|".join(rep.keys()))
text = pattern.sub(lambda m: rep[re.escape(m.group(0))], text)
For example:
>>> pattern.sub(lambda m: rep[re.escape(m.group(0))], "(condition1) and --condition2--")
'() and --text--'
You could just make a nice little looping function.
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
where text is the complete string and dic is a dictionary — each definition is a string that will replace a match to the term.
Note: in Python 3, iteritems() has been replaced with items()
Careful: Python dictionaries don't have a reliable order for iteration. This solution only solves your problem if:
- order of replacements is irrelevant
- it's ok for a replacement to change the results of previous replacements
Update: The above statement related to ordering of insertion does not apply to Python versions greater than or equal to 3.6, as standard dicts were changed to use insertion ordering for iteration.
For instance:
d = { "cat": "dog", "dog": "pig"}
my_sentence = "This is my cat and this is my dog."
replace_all(my_sentence, d)
print(my_sentence)
Possible output #1:
"This is my pig and this is my pig."
Possible output #2
"This is my dog and this is my pig."
One possible fix is to use an OrderedDict.
from collections import OrderedDict
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
od = OrderedDict([("cat", "dog"), ("dog", "pig")])
my_sentence = "This is my cat and this is my dog."
replace_all(my_sentence, od)
print(my_sentence)
Output:
"This is my pig and this is my pig."
Careful #2: Inefficient if your text string is too big or there are many pairs in the dictionary.
Is there an easier way to replace multiple different things at once
Feature Proposal: Multi-String Replacement Using a Dictionary in the .replace() Method - Ideas - Discussions on Python.org
Which is the better way to include multiple '.replace()' ?
how to replace multiple characters in one go with the .replace() function?
I have a string formatted timestamp that I would like to replace some characters.
Which is the better way to do this (of the two I have thought of!) ?
1 -
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", t_obj).replace("-", "").replace(" ", "_").replace(":", "")2 -
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", t_obj)
timestamp = timestamp .replace("-", "")
timestamp = timestamp .replace(" ", "_")
timestamp = timestamp .replace(":", "")First approach reads nicer but wouldn't readability wouldn't scale as well if I wanted to do more replaces.
Second one, is using the same variable name over and over like that until it's in the state you want considered bad practice maybe?
Cheers !
Hello, I would like to replace a certain letter in a string with a '*' (don't ask lol)
The problem is it will only either do it for the lower case letters or upper case letters.
Is there a way to get the function to replace both upper and lower case letters or am I going to need to use the function twice?
Thanks