file reading by record separator (not line by line)

Tijs tijs_news at bluescraper.nl
Fri Jun 1 06:08:12 EDT 2007


Steve Howell wrote:

> 
> 
> I think itertools.groupby() is usually the key
> batteries-included component in elegant solutions to
> this problem, but I wonder if the Python community
> couldn't help a lot of newbies (or insufficiently
> caffeinated non-newbies) by any of the following:
> 

Well, I'm not a newbie, and I always make sure to be thoroughly caffeinated
before sitting down for coding. But I think the itertools example in the
parent post shows a problem with that package: unless you have intimate
knowledge of the package, it is not clear what the code does when reading
it. In other words, it is not intuitive. 

Perhaps a dedicated "block-read" module that wraps any read()-able object
would be better. At least it would immediately be clear what the code
means.

from blockread import BlockReader

b = BlockReader(f, boundary='>')
for block in b:
        # whatever

-- 

Regards,
Tijs



More information about the Python-list mailing list