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.iteritems()) #Python 3 renamed dict.iteritems to dict.items so use rep.items() for latest versions 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--'
Related Posts:
- Renaming column names in Pandas
- Replace ‘ with \’ in a string
- Print string to text file
- TypeError: ‘float’ object is not subscriptable
- TypeError: ‘float’ object is not subscriptable
- Finding and replacing elements in a list
- Replace values in list using Python
- Best way to replace multiple characters in a string?
- How to display text in pygame?
- Where does this come from: -*- coding: utf-8 -*-
- Best way to replace multiple characters in a string?
- Replace and overwrite instead of appending
- How to read a text file into a list or an array with Python
- How can I create a text input box with Pygame?
- Using replace() method in python by index
- How to easily print ascii-art text?
- Remove specific characters from a string in Python
- pandas replace multiple values one column
- python capitalize first letter only
- Is there a short contains function for lists?
- ImportError: No module named pandas. Pandas installed pip
- How to Execute a Python Script in Notepad++?
- ValueError: operands could not be broadcast together with shapes (5,) (30,)
- appending list but error ‘NoneType’ object has no attribute ‘append’
- Using Python 3 in virtualenv
- “Series objects are mutable and cannot be hashed” error
- Putting a simple if-then-else statement on one line [duplicate]
- matplotlib savefig() plots different from show()
- Converting list to numpy array
- How do I parse a string to a float or int?
- Why do I get AttributeError: ‘NoneType’ object has no attribute ‘something’?
- What is __pycache__?
- Permission denied error by installing matplotlib
- Python equivalent of a given wget command
- How to find list intersection?
- Error while Executing jupyter notebook: [Errno 2] No such file or directory
- ValueError: max() arg is an empty sequence
- How do I insert a JPEG image into a python Tkinter window?
- Use a.any() or a.all()
- What is the difference between Spyder and Jupyter?
- What is the maximum recursion depth in Python, and how to increase it?
- subprocess.check_output return code
- Inverse of a matrix using numpy
- NameError: name ‘self’ is not defined
- gaierror: [Errno 8] nodename nor servname provided, or not known (with macOS Sierra)
- How to print colored text to the terminal
- TypeError: can only concatenate list (not “str”) to list
- What’s the idiomatic syntax for prepending to a short python list?
- What is the difference between “is None” and “== None”
- Why Python 3.6.1 throws AttributeError: module ‘enum’ has no attribute ‘IntFlag’?
- Creating 2D dictionary in Python
- Python: slicing a multi-dimensional array
- Python: pandas merge multiple dataframes
- Shuffle an array with python, randomize array item order with python
- How to count the NaN values in a column in pandas DataFrame
- How to install multiple python packages at once using pip
- Errno 13 Permission denied Python
- How to get all possible combinations of a list’s elements?
- Converting strings to floats in a DataFrame
- Pylint “unresolved import” error in Visual Studio Code
- Errno 13 Permission denied Python
- ValueError: object too deep for desired array while using convolution
- How to convert string to binary?
- How to iterate through two lists in parallel?
- Make list of arrays in python
- In Python, what is `sys.maxsize`?
- Pyspark: Exception: Java gateway process exited before sending the driver its port number
- Converting from a string to boolean in Python?
- Import Error: No module named numpy Anaconda
- How to remove \n from a list element?
- Breaking out of nested loops
- Youtube_dl : ERROR : YouTube said: Unable to extract video data
- How to stop a function
- What is the common header format of Python files?
- Is it possible to decompile a compiled .pyc file into a .py file?
- Writing string to a file on a new line every time
- Creating lowpass filter in SciPy – understanding methods and units
- Python 2: AttributeError: ‘list’ object has no attribute ‘strip’
- Get Confusion Matrix From a Keras Multiclass Model
- AttributeError: ‘list’ object has no attribute ‘lower’ gensim
- How to get the text cursor position in Windows?
- python NameError: global name ‘__file__’ is not defined
- Rename specific column(s) in pandas
- How to create a numpy array of lists?
- Scikit-learn GridSearch giving “ValueError: multiclass format is not supported” error
- Convert R code into Python code using rpy2
- PIL: DLL load failed: specified procedure could not be found
- “Cannot access setup.py: No such file or directory” – can’t run any .py files?
- How to delete a specific line in a file?
- How does ajax work with python?
- RSA encryption and decryption in Python
- how to check which version of nltk, scikit learn installed?
- How do I add two sets?
- How to count the number of files in a directory using Python
- Counting array elements in Python
- IPython, “name ‘plt’ not defined”
- Building a list inside a list in python
- ‘pyuic5’ is not recognized as an internal or external command
- ValueError: multiclass format is not supported
- Filtering a NumPy Array