scanf style parsing

Andrew Dalke dalke at dalkescientific.com
Wed Sep 26 02:28:37 EDT 2001


Bruce Dawson:
>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)
>
>In C/C++ that would be something like:
>sscanf(buffer, "smtpmail.exe - %d error(s), %d warning(s)", &errors,
>&warnings);

The usual solution is to use regular expressions for this.

>>> import re
>>> text = "smtpmail.exe - 0 error(s), 5 warning(s)"
>>> m = re.match(r"smtpmail.exe - (\d+) error\(s\), (\d+) warning\(s\)",
...              text)
>>> m.group(1), m.group(2)
('0', '5')
>>>

See the documentation for the 're' module.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list