[Tutor] Re: Starting to write a scanf-like module for Python

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Aug 15 09:06:06 CEST 2004



On Sun, 15 Aug 2004, Hans Nowak wrote:

> Danny Yoo wrote:
>
> > 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*
>
> Looks interesting, but I'm not sure if use raw_input is used all that
> often.  This would be extra useful if it could read from arbitrary
> files.  Maybe something like
>
> f = open("blah.txt", "r")
> # I want/expect a string and three integers
> s, i1, i2, i3 = fscanf(f, "%s %d %d %d")


Hi Hans,


###
>>> import scanf
>>> scanf.fscanf
<function fscanf at 0x74030>
>>> f = open("/usr/share/dict/words")
>>> scanf.fscanf(f, "%s")
('A',)
>>> scanf.fscanf(f, "%s")
('a',)
>>> scanf.fscanf(f, "%s")
('aa',)
>>> scanf.fscanf(f, "%s")
('aal',)
>>> scanf.fscanf(f, "%s")
('aalii',)
>>> scanf.fscanf(f, "%s")
('aam',)
>>> scanf.fscanf(f, "%s")
('Aani',)
>>> scanf.fscanf(f, "%s")
('aardvark',)
>>> scanf.fscanf(f, "%s")
('aardwolf',)
###


*grin*



More information about the Tutor mailing list