16

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.

Rob Avery IV
  • 3,562
  • 10
  • 48
  • 72
  • If your first line equals 'foo' (might want to change that to `if line.rstrip() == 'foo')`, does this not already on the 2nd iteration, print the 5th line? – Jon Clements Nov 26 '12 at 20:05
  • `line.next()` will not work in py 3.x , consider using `next(line)`, works in both. Though `next(line)` might not work in ancient python versions 2.5 or earlier. – Ashwini Chaudhary Nov 26 '12 at 20:09

5 Answers5

15

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. lists).

mgilson
  • 300,191
  • 65
  • 633
  • 696
2

Depending on the type of object that rofile is, I can think of a couple of ways to do this.

List of strings

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]

Iterable

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
jdotjdot
  • 16,134
  • 13
  • 66
  • 118
1

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
PeterBB
  • 343
  • 2
  • 9
  • OP _does_ want to do something for every line, though--note the `print line` statement right under the `for` loop. – jdotjdot Nov 26 '12 at 20:12
  • This probably would be a better implementation. Thanks!! – Rob Avery IV Nov 26 '12 at 20:15
  • @jdotjdot: See the sentence immediately after the sample: "When I come back around and loop for the second time, that first print statement should print the 5th line in the file." Some lines aren't supposed to hit that `print line` statement, because they're taken care of by the `if foo` block. – PeterBB Nov 26 '12 at 20:17
  • @PeterBB Yes, but ultimately, they're all hit by a `print` statment, since even in the `if foo` block, the lines go to `print`. Maybe that's just his generalized example, though. – jdotjdot Nov 26 '12 at 20:18
0

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
0

I had a similar problem and using the continue statement instead worked in my case