scanf style parsing

Quinn Dunkan quinn at ngwee.ugcs.caltech.edu
Wed Oct 3 23:48:07 EDT 2001


On Wed, 26 Sep 2001 22:21:05 GMT, Andrei Kulakov <sill at optonline.net> wrote:
>On Wed, 26 Sep 2001 05:42:45 GMT, Bruce Dawson <comments at cygnus-software.com> wrote:
>> I love programming in Python, but there are some things I have not found
>> the easy way to do. I understand that Python is supposed to be good at
>> text parsing, but I am having trouble with this simple task. Given this
>> text (the output from VisualC++) I want to find out how many errors and
>> warnings there were:
>> 
>> smtpmail.exe - 0 error(s), 0 warning(s)
>
>Seeing that there should only be 2 words here that are numbers, you can do
>this:
>
>my_str # that's where the result is
>words = my_str.split()
>lst = []
>for word in words:
>    try:
>        result = int(word)
>        lst.append(result)
>    except:
>        pass
>
>errors = lst[0]
>warnings = lst[1]

More compactly:

errors, warnings = [ int(w) for w in line.split() if w.isdigit() ]



More information about the Python-list mailing list