Frankenstring

Scott David Daniels Scott.Daniels at Acm.Org
Tue Jul 12 17:38:03 EDT 2005


Thomas Lotze wrote:
> Hi,
> I think I need an iterator over a string of characters pulling them out
> one by one, like a usual iterator over a str does. At the same time the
> thing should allow seeking and telling like a file-like object:
> 
> 
>>>>f = frankenstring("0123456789")
>>>>for c in f:
> 
> ...     print c
> ...     if c == "2":
> ...         break
> ... 
OK, frankenstring can be approximated by nothing:
for c in "0123456789":
     print c


Now if you want to do it for a file, you could do:

     for c in thefile.read():
         ....

Or, if you did not want to do a full read:
     def filechars(afile):
         for line in afile:
             for char in line:
                 yield char

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list