Hi stackoverflow Users,
I am wondering how to use for loop with string.
For example,
There is a file (file.txt) like,
=====================
Initial Value
1 2 3
3 4 5
5 6 7
Middle Value <---From Here
3 5 6
5 8 8
6 9 8 <---To Here
Last Value
5 8 7
6 8 7
5 5 7
==================
I want to modify the section of the file only in "Middle Value" and write an output file
after modifying.
I think that if I use "if and for" statements, that might be solved.
I have thought a code like
with open('file.txt') as f, open('out.txt', 'w') as f2:
for line in f:
sp1 = line.split()
line = " ".join(sp1) + '\n'
if line == 'Middle':
"Do something until line == 'Last'"
I am stuck with "Do something until line == 'Last'"
part.
Any comments are appreciated.
Thanks.