scanf in python...?

Steve Clift sgclift at earthlink.net
Thu Oct 19 01:53:56 EDT 2000


wware at world.std.com (Will Ware) wrote:
> Steve Clift (sgclift at earthlink.net) wrote:
> > ...the sscanf module I wrote about 5 years ago...
> 
> Whenever I need to do something like sscanf(), I use eval(). Is
> sscanfmodule.c much faster? It looks like a very good thing:
> http://www.python.org/ftp/python/contrib-09-Dec-1999/Misc/sscanfmodule.c

Well, it doesn't have to parse the input as an expression, and isn't
limited to inputs that are valid Python expressions. For example,
converting 10000 lines of 100 single-digit integers to a list:

	map(int, string.split(line)):	 3.5 secs
	sscanf(line, fmt):			 2.0 secs (where fmt = 100*'%d')
	eval(line):				39.4 secs (comma separated ints)

If the input lines are in a non-trivial format that is within sscanf's
capabilities, it is much faster and much more concise than the 'pure'
Python approach. There are some other bulk-input alternatives IIRC
(Numeric, undoubtedly, and I seem to recall a FORTRAN-style input formatter
somewhere).

Soapbox mode: FWIW, I think there is a significant disparity between
Python's data input and output facilities. It has good output formatting,
but nothing comparably concise for input. I often have to write one-off
programs to process large amounts of tabular output from other programs,
usually a mixture of floats and ints, with the occasional string for good
measure. Having to write a pile of tedious code to parse this sort of guff
is irritating, considering the relative ease with which you can accomplish
other things in Python. Still, few people seem to complain about this, so I
guess it's not a common issue.

-Steve






More information about the Python-list mailing list