Need help in extracting lines from word using python

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Mar 19 10:41:22 EDT 2013


On Tue, 19 Mar 2013 07:20:57 -0700, razinzamada wrote:

> I'm currently trying to extract some data between 2 lines of an input
> file using Python. the infile is set up such that there is a line
> -START- where I need the next 10 lines of code if and only if the -END-
> condition occurs before the next -START-. The -START- line occurs many
> times before the -END-. Heres a general example of what I mean:
> 
> blah
> blah
> -START-
> 10 lines I DONT need
> blah
> -START-
> 10 lines I need
> blah
> blah
> -END-
> blah
> blah
> -START-
> 10 lines I dont need
> blah
> -START-
> 
> .... and so on and so forth

[...]

> heres the code I have for printing the -START- + 10 lines:
> 
>     in = open('input.log')

No it is not. "in" is a reserved word in Python, that code cannot 
possibly work, it will give a SyntaxError.


Try this code. Untested but it should do want you want.


infile = open('input.log')
outfile = open('output.txt', 'a')
# Accumulate lines between START and END lines, ignoring everything else.
collect = False  # Initially we start by ignoring lines.
for line in infile:
    if '-START-' in line:
        # Ignore any lines already seen, and start collecting.
        accum = []
        collect = True
    elif '-END-' in line:
        # Write the first ten accumulated lines.
        outfile.writelines(accum[:10])
        # Clear the accumulated lines.
        accum = []
        # and stop collecting until the next START line
        collect = False
    elif collect:
        accum.append(line)

outfile.close()
infile.close()



-- 
Steven



More information about the Python-list mailing list