0

I have the lines:

for line in f:
    if 'ipsum' in next(f):    #just want to check
        print("Hello")

What I want is to only check what is there on the next line, not overall jump to the next line. What I want is something like:

for line in f:
    if 'ipsum' in next(f):    #just checking
        print("Hello")
        next(f) #then actually jump to the next line

Is there any other way to do this?

Irfan Sindhi
  • 75
  • 1
  • 8
  • ''' junk.txt is: one two three ... ten ''' import sys filehandle = open("junk.txt", "r") lines = filehandle.readlines() currentline = 0 for line in lines: if currentline == len(lines) - 1: continue nextline = currentline + 1 if "three" in lines[nextline]: print (line) currentline += 1 #tried to answer this, wrote it up and now can't submit an answer :( This works. – sniperd Nov 09 '17 at 19:02

1 Answers1

0

I am afraid you can not do that this way. I think you will have to use a list to store the lines of your file. You are using an iterator and you can not see what is next without actually calling next()

lpozo
  • 598
  • 2
  • 9