[Tutor] scanf-1.0 tested and about to release

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Aug 20 08:12:54 CEST 2004

> I thought it might make a nice weekend project to write scanf for
> Python; it's nowhere near done yet, but it's sorta working... *grin*
>     http://hkn.eecs.berkeley.edu/~dyoo/python/scanf.py

Hi everyone,

Ok, I've gotten everything implemented that I really wanted to implement.
If anyone wants to play with this 'scanf' module before I formally
announce it on comp.lang.python and PyPI, here's a link to the latest


I'll eventually write a web page for it, but my fingers are tired, so I'd
better take a break.

Here's some of what it does:

>>> import scanf
>>> scanf.sscanf("3.1415926 seventeen 42! Hut!", "%f %s %d")
(3.1415926000000001, 'seventeen', 42)
>>> from StringIO import StringIO
>>> sampleFile = StringIO("""
... 3, 17
... 4, 19
... 1, -5
... 16, 25""")
>>> scanf.fscanf(sampleFile, "%d, %d")
(3, 17)
>>> scanf.fscanf(sampleFile, "%d, %d")
(4, 19)
>>> scanf.fscanf(sampleFile, "%d, %d")
(1, -5)
>>> scanf.fscanf(sampleFile, "%d, %d")
(16, 25)
>>> scanf.fscanf(sampleFile, "%d, %d")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "./scanf.py", line 350, in fscanf
    return bscanf(buffer, formatString)
  File "./scanf.py", line 362, in bscanf
    return parser(buffer)
  File "./scanf.py", line 523, in __call__
    raise IncompleteCaptureError, (e, tuple(results))
scanf.IncompleteCaptureError: (<scanf.FormatError instance at 0x92300>, ())

At least this should help wean the Python beginners who were recovering C
programmers in a past life.  *grin*

Hope this amuses someone out there!

More information about the Tutor mailing list