I'm trying to remove every word that starts with a certain string in a text file. I'm stuck on how to write to the output file.
Input file:
Lorem ipsum applePEAR
dolor appleBANANA sit
appleORANGE amet, consectetur
Desired output file:
Lorem ipsum
dolor sit
amet, consectetur
My approach so far:
with open(infile) as fin, open(outfile, "w+") as fout:
for line in fin:
ls = line.split()
for word in ls():
if word.startswith("apple"):
line.replace(word, "")
fout.write(line)
I think the problem with this approach is replacing words in the line split list, not the line itself.
Checking Stackoverflow, I see this problem is similar to: using Python for deleting a specific line in a file, except the "nickname_to_delete" is a word that starts with a string.