Wednesday 15 September 2010

python - Extract from current position until end of file -



python - Extract from current position until end of file -

i want pull info text file specified line number until end of file. how i've tried:

def extract_values(f): line_offset = [] offset = 0 last_line_of_heading = false if not last_line_of_heading: line in f: line_offset.append(offset) offset += len(line) if whatever_condition: last_line_of_heading = true f.seek(0) # non-functioning pseudocode follows info = f[offset:] # read current offset end of file variable

there blank line between header , info want, ideally skip also.

do know line number in advance? if so,

def extract_values(f): line_number = # info = f.readlines()[line_number:]

if not, , need determine line number based on content of file itself,

def extract_values(f): lines = f.readlines() line_number, line in enumerate(lines): if some_condition(line): info = lines[line_number:] break

this not ideal if files enormous (since lines of file loaded memory); in case, might want in 2 passes, storing file info on sec pass.

python python-2.7

No comments:

Post a Comment