i am trying to replace text in file from a list in python, but got stuck and need some suggestion.
Basically, i am looking for text in replace_texts in each line of the file then save it using fileinput.
replace_texts = ['text 1','text 2']
for line in fileinput.input('file.txt', inplace=True):
if any(text in line for text in replace_texts):
# im not sure how to get the value variable from the if**
replaced_line = line.replace(value, "something else")
sys.stdout.write(replaced_line)
else:
sys.stdout.write(line)
file.txt
abctd fdskla fds
text 1 fdlald text 2
fdsa text 2
and after run the code above. file.txt will become
abctd fdskla fds
something else fdlald something else
fdsa something else
Thanks,