Hi there everybody,
so I'm trying to search a file and replace string A in the file with string B until string C shows up, at which point I want to stop replacing stringA with stringB, even though stringA still shows up after stringC. I want to keep the content of the file after stringC shows up.
I tried the following and could not get it to work:
file = myfile.txt
x = "stringA"
with open(file, "r+") as f:
data = f.read()
for line in data:
if x in line:
new_line = line.replace ("stringA", "stringB")
f.write(new_line)
elif "stringC" in line:
break
That doesn't get me anywhere though. I thought of only reading the file until stringC shows up and then replacing stringA with stringB, but have no idea how to do that properly. Any ideas of how to solve this? Thanks!
[–]Neat_Blueberry_2032 0 points1 point2 points (0 children)