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 Andrew Dalke other posts by this author
Sep 26 2001 6:28AM messages near this date
Re: scanf style parsing | Re: Keeping os context possible? Re: Python a good thing for sysadmins ?
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@[...].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