scanf style parsing

Andrei Kulakov sill at optonline.net
Wed Sep 26 18:21:05 EDT 2001


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]

I'm not sure how can this format vary, but if these numbers are always at
the same place, you could have just done:

errors, warnings = int(words[2]), int(words[4])

- Andrei 

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org



More information about the Python-list mailing list