Newbie Questions: Swithing from Perl to Python

Mike C. Fletcher mcfletch at rogers.com
Sat Oct 25 23:02:40 EDT 2003


Luther Barnum wrote:
...

>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}++;
>    }
>}
>  
>
Haven't tried running this, but should give you an idea of how the 
equivalent would work in Python...

import re, sys
server_totals = {}
error_totals = {}
for line in sys.stdin:
    line = line.strip()
    match = re.match( """
        \w+
        \s+
        \d+
        \s+
        (\w+)
        \s+
        (\w+)""", re.X )
    if match:
       server, error = match.group(1), match.group(2)
       server_totals[server] = server_totals.get( server, 0) + 1
       error_totals[error] = error_totals.get( server, 0) + 1

HTH,
Mike

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list