Iterate over text file, discarding some lines via context manager

Michael Torrie torriem at gmail.com
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
            continue
        if line.startswith( '#' ):
            # discard lines starting with #
            continue
        items = line.split( )
        if not items:
            # discard lines with only spaces, tabs, etc
            continue
	yield items

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

For an excellent presentation on this sort of thing, see:
http://www.dabeaz.com/generators/



More information about the Python-list mailing list