I would like to loop on every lines from a .txt file and then use re.sub method from Python in order to change some contents if there is a specific pattern matcing in this line.
But how can I do that for two different pattern in the same file ?
For example, here's my code :
file = open(path, 'r')
output = open(temporary_path, 'w')
for line in file:
out = re.sub("patternToChange", "patternToWrite", line) #Here I would like to also do the same trick with "patternToChange2 and patternToWrite2 also in the same file
output .write(out)
Do you have any ideas ?
patternToChangeand replace it withpatternToWrite.re.subagain, but takingoutas its input, and using your new patterns? If you saved the result back toout, you wouldn't need to make any other changes.re.sub(...).sub(...)in one line? If not, Scott's idea is correct where you would doout = re.sub(pattern1, write1, line)thenout = re.sub(pattern2, write2, out)