Date: Fri, 17 Nov 1995 22:30:38 +1100 From: Steve Clift To: python-list@cwi.nl Subject: Python sscanf() look-alike. If you miss having a fast and dirty way to scan stuff out of Python strings, a-la C's sscanf() function, help may be at hand. I've uploaded a beta version of a sscanf() look-alike module to ftp://ftp.python.org/incoming, from where it should soon emerge. >From the comprehensive documentation at the beginning of the source file :-): /* * This is a simple C sscanf() analog. sscanf(input, format) parses * the input string according to the format specification and returns * a list of the converted values. The C sscanf() assignment * suppression and maximum field width syntax is supported. An 'l' * size modifier causes integer conversions to return a Python * long rather than a Python integer; it has no effect for float * conversions. * * Conversion specification are of the form: * %[*][]['l']. * * The following format conversions are supported: * * %c Fixed width character string. * %d Signed integer (leading 0 => octal, 0x => hex). * %f, %g, %e Python float (C double). * %i Same as '%d'. * %l Python long. * %n Number of characters parsed so far. * %o Octal integer. * %s String of non-whitespace characters with leading * whitespace skipped. * %u Unsigned integer. * %x Hexadecimal integer. * %[] Character scan set. * * Parsing of the format string stops when the input string is exhausted, * so format conversion syntax errors will go undetected until there is * enough input to reveal them. * * There are some differences from C sscanf(): * * 1) The %c conversion scans a fixed-width (default 1) character string * rather than a single character, with no special interpretation of * whitespace. For example, '%5c' converts the next 5 characters or * the remainder of the input string, whichever is the shorter, into * a Python string. Some C sscanf() implementations also work this * way. * * 2) If a field width is specified, it sets the maximum number of * characters, starting from the current position, that will be * considered in the conversion. C sscanf() conversions tend to * skip white space before imposing the field width. You can * get the C sscanf() behaviour by inserting a space before the * format specification. * * To install the module in Python, copy the source file into the * Modules directory, add the following line to the Modules/Setup * file: * sscanf sscanfmodule.c * and do a make. * */ - ---------------------------------------------------------------------------- Steve Clift Email: clift@ml.csiro.au CSIRO Division of Oceanography Phone: +61-02-32-5243 Hobart, Tasmania, Australia Fax: +61-02-32-5000 - ---------------------------------------------------------------------------- .