ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-list
python-list
Re: scanf style parsing
by Skip Montanaro other posts by this author
Sep 26 2001 6:14AM messages near this date
Re: scanf style parsing | Re: scanf style parsing
Bruce>  I want to find out how many errors and warnings there were:

    Bruce>  smtpmail.exe - 0 error(s), 0 warning(s)

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

    Bruce>  It's not that I think the sscanf syntax is particularly elegant,
    Bruce>  but it sure is compact! I saw the discussion about adding scanf
    Bruce>  to Python

    Bruce>  but I need to know what people do right now when faced with this
    Bruce>  task.

This is a task often relegated to regular expressions, which probably
explains (in part) why scanf is not found in python.  You could define a
regular expression like

    errpat = re.compile(r'(?P<exe> [^\s]+)\s+-\s+'
                        r'(?P<err> \d+)\s+error\(s\),\s+'
                        r'(?P<warn> \d+)\s+warning\(s\)')

then use it to extract the program name, number of errors and number of
warnings like so:

    mat = errpat.match(line)
    if mat is not None:
        exe = mat.group('exe')
        nerrs = int(mat.group('err')
        nwarns = int(mat.group('warn')

It's not nearly as compact as a similar scanf pattern, but regular
expressions are considerably more powerful.

HTH,

-- 
Skip Montanaro (skip@pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/

-- 
http://mail.python.org/mailman/listinfo/python-list
Thread:
Bruce Dawson
Skip Montanaro
George Demmy
Hans-Peter Jansen
Quinn Dunkan
Tim Hammerquist
Ralph Corderoy
Toby Dickenson
Duncan Booth
Aahz Maruch
Aahz Maruch
Aahz Maruch
Stefan Schwarzer
Grant Edwards
Fredrik Lundh

Malcolm Tredinnick
Ralph Corderoy
Tim Hammerquist
Stefan Schwarzer
Greg Ewing
Skip Montanaro
Boyd Roberts
Steve Clift
Bruce Dawson
Tim Hammerquist
Tim Hammerquist
Tim Hammerquist
Skip Montanaro
Andrew Dalke
Fredrik Lundh
Oleg Broytmann
Andrei Kulakov
Duncan Booth
Chris Barker
Tim Hammerquist
Skip Montanaro
Jon Nicoll
Oleg Broytmann
Bruce Dawson
Skip Montanaro
Andrei Kulakov
Richard Jones
Skip Montanaro
Andrew Dalke

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved