Newbie Questions: Swithing from Perl to Python

Luther Barnum SpamSucks at rr.com
Sat Oct 25 23:48:29 EDT 2003


Actually it could have been written using split. Using regular expressions
makes it a little more flexible. Python has that so that is not the issue
really. It's just that I read that you cannot change a dictionary value and
I wanted to see how it was done in Python. My last question is how do you
iterate over this to get the values by key.

 While(<FILE>) {
     chomp;
     @line = split;
         $server = $3;
         $error = $4;

         $server_totals{$server}++;
         $error_totals{$error}++;
     }
 }


Thanks in advance, you guys have been very helpful

Luther


"Mark Roach" <mrroach at okmaybe.com> wrote in message
news:pan.2003.10.26.03.22.29.325971 at okmaybe.com...
> On Sun, 26 Oct 2003 02:14:19 +0000, Luther Barnum wrote:
> [...]
> > Here is  another example:
> >
> > ex:
> >
> > While(<FILE>) {
> >     chomp;
> >     if(/(\w+    # Date
> >     \s+          # Space
> >     \d+         # Day
> >     \s+          # Space
> >     (\w+)         # Server
> >     \s+          # Space
> >     (\w+)/x) {  # Error
> >
> >         $server = $1;
> >         $error = $2;
> >
> >         $server_totals{$server}++;
> >         $error_totals{$error}++;
> >     }
> > }
>
> Whew, I remember now why I ran away from perl fairly quickly. Can you
> explain what the above code does? I can see that it iterates over a file
> and that somehow it extracts a server name and an error, but I have no
idea
> what all the strange variables are... I am guessing that is a regex?
>
> If what you are describing is reading from a file formatted like:
> 2003/10/25 Sat Servername Error10
>
> then something like this might be what you are looking for:
>
> server_totals = {}
> error_totals = {}
> for line in file('/path/to/logfile'):
>     line = line.strip()
>     date, day, server, error = line.split()
>     server_totals[server] = server_totals.get(server, 0) + 1
>     error_totals[error] = error_totals.get(error, 0) + 1
>
> -Mark
>






More information about the Python-list mailing list