[Tutor] Introduction - log exercise

Antonio de la Fuente toni at muybien.org
Tue Nov 17 21:36:40 CET 2009

* Nick Stinemates <nick at stinemates.org> [2009-11-17 13:30:51 -0500]:

> Date: Tue, 17 Nov 2009 13:30:51 -0500
> From: Nick Stinemates <nick at stinemates.org>
> To: Antonio de la Fuente <toni at muybien.org>
> Cc: Python Tutor mailing list <tutor at python.org>
> Subject: Re: [Tutor] Introduction - log exercise
> Mail-Followup-To: Antonio de la Fuente <toni at muybien.org>,
> 	Python Tutor mailing list <tutor at python.org>
> User-Agent: Mutt/1.5.20 (2009-06-14)
> Message-ID: <20091117183051.GA20117 at stinemates.org>
> > I will read lines from file, with the 'for loop', and then I will check them for
> > 'foo' matches with a 'while loop', if matches I (somehow) re-initialise the
> > list, and if there is no matches for foo, I will append line to the list. When I
> > get to a blank line (end of block), write myList to an external file. And start
> Can you please explain what you mean by _re-initialize the list_ ?

By re-initialize the list I was thinking to get rid of the lines that
have been put into it, until it has been found one with 'foo', if not
it will contaminated the new log file, with lines from blocks that had
'foo' lines.

> > with another line.
> > 
> > I am stuck with defining 'blank line', I don't manage to get throught the while
> > loop, any hint here I will really appreciate it.
> > I don't expect the solution, as I think this is a great exercise to get wet
> > with python, but if anyone thinks that this is the wrong way of solving the
> > problem, please let me know.
> > 
> > 
> Can you explain why the following won't work?

Because I don't know how to define a blank line, that will allow me to
differentiate between blocks.

> #!/usr/bin/python
> import sys
> import gzip
> # At the moment not bother with argument part as I am testing it with a
> # testing log file
> #fileIn = gzip.open(sys.argv[1])
> fileIn = gzip.open('big_log_file.gz', 'r')
> fileOut = open('outputFile', 'a')
> for line in fileIn:
>     while line != 'blank_line':
>         if 'foo' not in line:
>             fileOut.write(line)

Antonio de la Fuente Martínez
E-mail: toni at muybien.org

El que con niños se acuesta, mojado se levanta. 

More information about the Tutor mailing list