Iterate over text file, discarding some lines via context manager

Michael Torrie torriem at
Fri Nov 28 19:24:07 CET 2014

On 11/28/2014 08:04 AM, fetchinson . wrote:
> Hi all,
> I have a feeling that I should solve this by a context manager but
> since I've never used them I'm not sure what the optimal (in the
> python sense) solution is. So basically what I do all the time is
> this:

I'd personally do it with a generator function.

def filter(input):
    for line in input:
        if not line:
            # discard empty lines
        if line.startswith( '#' ):
            # discard lines starting with #
        items = line.split( )
        if not items:
            # discard lines with only spaces, tabs, etc
	yield items

for line_items in filter(open( 'myfile' )):
    process( items )

For an excellent presentation on this sort of thing, see:

More information about the Python-list mailing list