[Tutor] Conditional output of a header line

Sean 'Shaleh' Perry shalehperry@attbi.com
Tue, 24 Sep 2002 13:06:36 -0700


On Tuesday 24 September 2002 12:37, Levy Lazarre wrote:
> Good afternoon all,
>
> I have written a script that cleans an error log and
> writes the output to a
> new file. Basically, I read the file line by line,
> apply various filters to
> eliminate the trashy lines, and output the desired
> lines to a new file via
> print statements. I am having some difficulty with one
> last task however.
> I need to conditionally eliminate a header line
> depending on what comes on
> the next line.
> Please see the sample below. When a line starts with
> "Console Log" I need to
> 'look ahead' and see if it is followed by a time stamp
> hh:mm:ss. If so,
> output it. If not suppress it. In the example
> enclosed, I would want to output
> the first "Console Log ..." line but ignore the second
> one.
>


last_line =3D ''
for line in lines:
    if line is console log line:
        last_line =3D line
    elif last_line and this line has valid time stamp:
        output last_line
        last_line =3D ''

Now this code is missing some piece like 'line is console log line' needs=
 to=20
be implemented.  But the idea should get you rolling.