Newbie Questions: Swithing from Perl to Python

Luther Barnum SpamSucks at rr.com
Sat Oct 25 22:14:19 EDT 2003


I see your from the Tampa area also, cool. That part seems pretty easy but
what I'm looking for is incrementing a counter. I use this all the time for
summarizing log files. I would probably prefer to keep using Perl but I work
in a place where Python is used much more than Perl so I want to learn it
the Python way.

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}++;
    }
}

With this code, I now have a hash that will total each type of error and
server. If I can concur this in Python, I will be extremely happy. I can
learn the rest over time but  this is something that I use constantly as I
am a Unix Administrator.

Luther


"Todd Stephens" <huzzah at tampabay.rr.com> wrote in message
news:pan.2003.10.26.01.52.23.855415.55687 at tampabay.rr.com...
> On Sat, 25 Oct 2003 21:34:23 -0400, Luther Barnum wrote:
>
> > 2. How can I sort and print out a hash.
> >
> > Example in Perl:
> >
> > ex. foreach $string (sort keys %hash) {
> >      print("$string = $hash{$string}\n");
> >      }
> >      }
>
> Well, as a Python learner myself, I am going to attempt this for my own
> education as well.  I think you are looking for a dictionary in Python.
> Let's say you have a dictionary 'dict' that contains something like this:
>
> >>> dict = {'a':'me', 'b':'myself', 'c':'I'}
>
> To print the dictionary as you iterate over it is simple:
>
> >>> for key in dict:
> ...   print key, '=', dict[key]
>
> This gives me:
>
> a = me
> c = I
> b = myself
>
> The order it prints could vary each time.  I am not sure how to print a
> sorted list from a dictionary.  I think this would probably involve
> assigning the dictionary elements to a list, then printing the sorted
> list(s).  I would like to see the code for that myself.  BTW, Go Bucs.
>
> -- 
> Todd Stephens
>






More information about the Python-list mailing list