I am trying to edit a text file using fileinput.input(filename, inplace=1)
The text file has say 5 lines:
line 0
line 1
line 2
line 3
line 4
I wish to change data of line 1
based on info in line 2
.
So I use a for
loop
infile = fileinput.input(filename, inplace=1)
for line in infile:
if(line2Data):
#do something on line1
print line,
else:
line1=next(infile)
line2=next(infile)
#do something with line2
Now my problem is after the 1st iteration the line
is set to line2
so in 2nd iteration the line is set to line3
. I want line to be set to line1
in 2nd iteration. I have tried line = line
but it doesn't work.
Can you please let me know how I am reset the iteration index on line
which gets changed due to next
PS: This is a simple example of a huge file and function I am working on.