import fileinput
with fileinput.FileInput('FILE_PATH_ON_MY_COMPUTER', inplace=True, backup='') as file:
for line in file:
print(line.replace(", uh,", ""), end='')
for line in file:
print(line.replace(", uh", ""), end='')
for line in file:
print(line.replace(" uh,", ""), end='')
I am trying to use the fileinput library to find and replace the matching text in a txt file. I would like it to find and replace multiple strings, not just one. Thus, I tried to put it all into one program. However, when I have a separate for loop for each print statement (as shown in the code), it only replaces the first keyword. When I keep one for loop and have all the print lines under it, the resulting txt file is a massive mess of repeated lines vertically spaced far apart. How do I get the program to find and replace multiple items without failing? What is the logical error in my code?
Thanks for the help