Reputation: 3730
I have a file that I want to get each line at a time, but once it gets to a specific line, I need to get the next few lines information.
Here is a code sample:
rofile = open('foo.txt', 'r')
for line in rofile:
print line
if(line.strip() == 'foo'):
line = line.next()
print line
line = line.next()
print line
line = line.next()
print line
When I come back around and loop for the second time, that first print statement should print the 5th line in the file. Is there any possible way to do this?
EDIT: Sorry for not clarifying the details. rofile
is a file object that I'm iterating through. Whether next()
is the real method to obtain the next line when using a file, I don't know. I don't have much experience with file manipulation in python.
Upvotes: 16
Views: 59448
Reputation: 87
I had a similar problem and using the continue
statement instead worked in my case
Upvotes: 0
Reputation: 123473
Here's a simple way to do it for a file object:
with open('foo.txt', 'r') as rofile:
for line in rofile:
print line,
if line.strip() == 'foo':
for _ in xrange(3): # get the next 3 lines
try:
line = rofile.next()
except StopIteration:
break
print line,
The try/except is needed in case there aren't 3 more lines after a 'foo' line is seen.
Upvotes: 0
Reputation: 343
Don't use a for
loop if you don't actually want to do something for every line. One option might be:
try:
while True:
line = file.next()
#do stuff
if line == 'foo':
#do other stuff
except(StopIteration):
#go on with your life
Upvotes: 1
Reputation: 17052
Depending on the type of object that rofile
is, I can think of a couple of ways to do this.
If you can get it to be simply a list of strings that make up the lines of the file:
for index, line in enumerate(rofile):
if line == 'foo':
for a in range(index, index + HOW_MANY_LINES_YOU_WANT):
print rofile[a]
If the file is already an iterable:
for line in rofile:
print line
if line == 'foo':
for a in range(3): # Just do it 3 times
print line.next()
# After this happens and the for loop is restarted,
# it will print the line AFTER
You can see in this quickie example I wrote that it'll work this way as an iterable:
>>> k = iter([1,2,3,4])
>>> for a in k:
print 'start loop'
print a
if a == 2:
print 'in if'
print k.next()
print 'end if'
print 'end loop'
start loop
1
end loop
start loop
2
in if
3
end if
end loop
start loop
4
end loop
Upvotes: 3
Reputation: 309929
You can use iter
to convert your object into an iterable which supports next
.
irofile = iter(rofile)
for line in irofile:
print line
if(line == 'foo'):
line = next(irofile) #BEWARE, This could raise StopIteration!
print line
As pointed out in the comments, if your object is already an iterator, then you don't need to worry about iter
(this is the case with file
objects). However, I leave it here as it works for the case of any arbitrary iterable (e.g. list
s).
Upvotes: 16