Newbie Questions: Swithing from Perl to Python
Mark Roach
mrroach at okmaybe.com
Sat Oct 25 23:30:23 EDT 2003
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