You have already seen how we can search and replace words in a file with the help of regular expression. Now we will try to reuse the code so that we can use them again and again. Besides, we will also try to write them in a more human readable way.
Let us first write the steps. What we want to achieve is very important before achieving something. Let us have a clear idea first and the best way is writing them down.
1) We need to open a file and use the try block to avoid getting any error message. Beginners may find this ‘try block’ quite intimidating. I have not explained it before and suddenly start using it. I have done it intentionally. It is explained in the next chapter ‘Exceptions, Catching Errors’. But before that I want you trying them and get habituated to a concept that looks complex. Once you learn this ‘try block’ please revisit this code again. You will find it extremely easy! Moreover, as you progress, you will find that using ‘try block’ is always a good habit.
2) Get the pattern of the words that we want to search and using flags we can ignore case.
3) Use that “re” module search method to see if that pattern matches with our line.
4) Now if the pattern matches, you replace it with new words.
Consider this code below and read the comments. In the comments, I briefly explain what I am going to do.
files = open("../primary/file.txt")
# you can search any pattern that can match ignoring the upper or lower case
pattern = re.compile('(len|neverm)ore', re.IGNORECASE)
for line in files:
# re module search that pattern in a line
if re.search(pattern, line):
# we found that patetrn and now it is time to replace them with a new string
print(pattern.sub("######", line), end=' ')
except FileNotFoundError as e:
print("File was not found:", e)
if __name__ == "__main__":
And in the output it replaces all the words “lenore” and “nevermore” with six hash tags. To do that it also checks the upper and lower case and finally replace them all.
first line ######
third line and ###### over
fifth pine line ######
i don’t know ######