file reading by record separator (not line by line)

Steve Howell showell30 at yahoo.com
Fri Jun 1 06:23:20 EDT 2007


--- Tijs <tijs_news at bluescraper.nl> wrote:

> Steve Howell wrote:
> > [...] 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. 
> 

Agreed.

> 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
> 

Yep, I like this idea.  You might have a few
variations:

def simple_block_reader(f, start_char='>'):
   # returns list or iterator where each item
   # is a list of lines all belonging to the same 
   # block

def regex_block_reader(f, start_regex, 
        end_regex=None):
   # start_regex is regular expression to match on
   # if end_regex is none, then end of block is 
   # signified by start of next block or end of file

def block_reader3(f, start_method, end_method=None):
   # start_method, end_method should be functions
   # that evaluate line, return True/False
   # 
   # if end_method is None, then end of block is
   # signified by start of next block or end of 
   # input stream




       
____________________________________________________________________________________
Sick sense of humor? Visit Yahoo! TV's 
Comedy with an Edge to see what's on, when. 
http://tv.yahoo.com/collections/222



More information about the Python-list mailing list