If you've ever wanted to say:
well, now you can! *grin*import scanf print scanf.sscanf("/usr/sbin/sendmail - 0 errors, 4 warnings", "%s - %d errors, %d warnings")
All software releases of this package are licensed under the New BSD license.
scanf provides formatted input from standard input, strings, or files, using a format-string syntax that's similar to C's scanf(). The syntax should be familiar to C programmers, and offers very simple pattern matching against strings and files.
% [*] [width] [type]
d | decimal integer. |
i | integer. The integer may be in octal (leading zero) or hexadecimal (leading 0x or 0X). |
o | octal integer (with or without leading zero). |
x | hexadecimal integer (with or without leading 0x or 0X) |
c | characters. The next input characters (default 1) are placed at the indicated spot. The normal skip over white space is suppressed; to read the next non-white space character, use %1s. |
s | character string (not quoted). |
f | floating-point number with optional sign and optional decimal point. |
% | literal %; no assignment is made. |
There is a sscanfmodule.c extension module in contrib section of Python.org, although I'm not sure how fresh this is. There was also a fairly large thread about this on comp.lang.python, although it looks like the consensus was to recommend regular expressions.
There was also a discussion on Python-dev about adding another scanf-like operator, to offer symmetry with String Formatting. Although adding a new operator didn't appear to fly, it looks like people agreed that having a scanf module might be a good idea.
Back to python.