[Tutor] Introduction - log exercise

Wayne Werner waynejwerner at gmail.com
Tue Nov 17 18:41:25 CET 2009


On Tue, Nov 17, 2009 at 10:58 AM, Antonio de la Fuente <toni at muybien.org>wrote:

> Hi everybody,
>
> This is my first post here. I have started learning python and I am new to
> programing, just some bash scripting, no much.
> Thank you for the kind support and help that you provide in this list.
>

You're welcome!


>
> This is my problem: I've got a log file that is filling up very quickly,
> this
> log file is made of blocks separated by a blank line, inside these blocks
> there
> is a line "foo", I want to discard blocks with that line inside it, and
> create a
> new log file, without those blocks, that will reduce drastically the size
> of the
> log file.
>
> The log file is gziped, so I am going to use gzip module, and I am going to
> pass
> the log file as an argument, so sys module is required as well.
>
> 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
> 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.
>

Let me start out by saying this; I'm very impressed with the thought you've
put into the problem and the way you've presented it.

The first thing that pops into my mind is to simply strip whitespace from
the line and check if the line == ''. Upon further experiment there's the
"isspace" method:

In [24]: x = '   \n\n\r\t\t'

In [25]: x.isspace()
Out[25]: True

x contains a bunch of spaces, newlines, and tab chars. From the docs:

" Return True if all characters in S are whitespace
    and there is at least one character in S, False otherwise."



> 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.
> <snip some code>
> for line in fileIn:
>    while line != 'blank_line':
>        if line == 'foo':
>            Somehow re-initialise myList
>            break
>        else:
>            myList.append(line)
>    fileOut.writelines(myList)

 <snip some more>


Rather than using a while, you can use an if statement with the space method
(and join your statement with an:

if not line.isspace() and not line == 'foo':
    fileOut.write(line)

then you can get rid of the whole myList. Based on what you've written,
there's really no need to have a list, it's more efficient to just write the
line straight to the file.

for the renaming part, take a look at the shutil module.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091117/3a809265/attachment-0001.htm>


More information about the Tutor mailing list