scanf style parsing

Chris Barker chrishbarker at home.net
Thu Sep 27 13:53:57 EDT 2001


Bruce Dawson wrote:

> Rather than the long and complicated PEP route of adding scanf
> style functionality to Python it would probably be enough to just add some
> more examples to the Python regexp documentation. I searched the Python
> documentation for scanf() and I looked at the regexp documentation, so if
> it had contained the examples in this reply or others I would have done
> my parsing properly long ago.

I completely disagree: scanf-type functionality is by no means a
replacement for REs or vice-versa. I'm not all that familiar with scanf,
but from what I have seen, it is really only useful for very simple text
scanning, mostly to extract numbers from strings (or files). When scanf
would work well, Python's string functions would generally work just
fine (see the examples given in this thread--much easier than the re
methods!). When regexs are warranted, scanf would be useless.

That being said, I am a strong advocate of adding some kind of
scanf-type fuctionality to Python, so that it can be used for what it is
good for: extracting numbers from fairly rigidly formated text. This can
be done well with string methods. I have a lot of code similar to:

for line in file.xreadlines():
	data.append(map(float,line.split()))

This is kind of clunky, and very slow if you have a lot of data. I'd
really like a cleaner, faster way of doing this kind of thing. I know
there are some routines in SciPy that help, but a more native approach
would be nice.

-Chris



-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list