Any push back like function in python?

Question:

Sorry guys!!! pardon me. I’m a beginner in Python. I am writing the following code:

for line in file:
 if StartingMarker in line:
  # Here: I want to push back 'line' back in 'file'
    for nextline in file:
     if EndingMarker in line:
        # Do some Operation
print "Done"

How can I push ‘line’ back in ‘file’?

Thanks in Advance.

Asked By: Humble Learner

||

Answers:

for line in file:
    if StartingMarker in line and EndingMarker in line:
        # look ma! no need to push back

EDIT:

for line in file:
    if StartingMarker in line:
        # do some operation
        if EndingMarker in line:
            # do some other operation
Answered By: Lie Ryan
for line in file:
    if StartingMarker in line:
        if endingmarker in line:
            #do operation
        else:
            for nextline in file:
                if EndingMarker in line:
                     # Do some Operation
print "Done"
Answered By: oadams

Don’t push back, yield.

def starttoend(it):
  for line in it:
    if 'START' in line:
      yield line
      break
  for line in it:
    yield line
    if 'END' in line:
      break

l = ['asd', 'zxc', 'START123', '456789', 'qwertyEND', 'fgh', 'cvb']

i = iter(l)
for line in starttoend(i):
  print line

Just use the iterator again if you need more sequences.

There’s no iterator that I know of that you can start iterating over and then push an item that’s been taken back into the iterator. But what you can do is create a new iterator (with itertools.chain) that iterates over the current item and the remaining items in the original iterator. Something like:

import itertools

with open('some-input-file') as f:
    it = iter(f)
    for line in it:
        if StartingMarker in line:
            it2 = itertools.chain(iter([line]), it)
            for nextline in it2:
                if EndingMarker in nextline:
                    # Do some Operation
Answered By: snapshoe

This is 12 years old but I found another answer to this in python3

If you manually make an iterator for your file with iter(…) you can use iteratortools.tee to fork the iterator and make a local copy to continue from that point without changing the parent iterator. It is important that you tee 2 itrs and that you return the OTHER one for your main loop to continue with.

This does mean you will go over the local lines twice unless you consume the recognized lines from the one you are going to return. This is a useful function to do that

def consume(iterator, n):
'''Advance the iterator n-steps ahead. If n is none, consume entirely.'''
  collections.deque(itertools.islice(iterator, n), maxlen=0)

The other answer to this ofcourse is to write a line-buffering read that wraps your file and uses iter internally to be one line ahead of what it returns. This would be similar to the char lookahead in a pascal "file window"

Answered By: user430788
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.