start reading from certain line

Diez B. Roggisch deets at nospam.web.de
Wed Jul 9 06:53:19 EDT 2008


antar2 wrote:

> I am a starter in python and would like to write a program that reads
> lines starting with a line that contains a certain word.
> For example the program starts reading the program when a line is
> encountered that contains 'item 1'
> 
> 
> The weather is nice
> Item 1
> We will go to the seaside
> ...
> 
> Only the lines coming after Item 1 should be read

Start reading each line, and skip them until your criterion matches. Like
this:

def line_skipper(predicate, line_iterable):
    for line in line_iterable:
        if predicate(line):
           break
    for line in line_iterable:
        yield line

Diez



More information about the Python-list mailing list