python - Cancel last line iteration on a file -
i need iterate on file, stop iteration on condition , continue parse file @ same line function (that may change can't add content in previous function).
an example file (file.txt) :
1 2 3 4 5 6 7 8 9 function try :
def parse1(file, stop): # 1st parsing function (main function doing) line in file: if line.strip() == stop: # stop parsing on condition break else: # parse line (just print example) print(line) def parse2(file): # 2nd parsing function (will own functions or external functions) line in file: # parse line (just print example) print(line) result in terminal:
>>> file = open("file.txt") >>> parse1(file, "4") 1 2 3 >>> parse2(file) 5 6 7 8 9 my problem "4" line skipped 1st function when condition.
how can avoid : found solution cancel last iteration or go line.
the file.tell() function don't work for on file.
i tried while + file.readline() very slower for loop on file (and want parse files millions of lines).
is there elegant solution keeping use of for loop ?
in python3, 'for line in file' construct represented iterator internally. definition, value produced iterator cannot 'put back' later use (http://www.diveintopython3.net/iterators.html).
to desired behaviour, need function chains 2 iterators, such chain function provided itertools module. in stop condition of parse1, return last line file iterator:
import itertools def parse1(file,stop): # 1st parsing function line in file: # stop parsing on condition if line.strip() == stop: return itertools.chain([line],file) # important line else: # parse line (just print example) print('parse1: '+line) the chain statement connects 2 iterators. first iterator contains 1 element: line want process again. second iterator remaining part of file. first iterator runs out of values, second iterator accessed.
you don't need change parse2. clarity, modified print statement:
def parse2(file): # 2nd parsing function line in file: # parse line (just print example) print('parse2: '+line) then, can call parse1 , parse2 in functional manner:
with open('testfile','r') infile: parse2(parse1(infile,'4')) the output of above line is:
parse1: 1 parse1: 2 parse1: 3 parse2: 4 parse2: 5 parse2: 6 parse2: 7 parse2: 8 parse2: 9 note, how value '4' produced parse2 function.
Comments
Post a Comment