Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:text': 0.05; 'context': 0.07; 'discard': 0.07; 'subject:file': 0.07; "'#'": 0.09; 'caller': 0.09; 'line:': 0.09; 'python': 0.11; 'def': 0.12; 'at,': 0.16; 'empty,': 0.16; 'generator.': 0.16; 'line.split()': 0.16; 'lot!': 0.16; 'paradigm.': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'basically': 0.19; '(in': 0.22; 'space.': 0.24; 'cheers,': 0.24; "i've": 0.25; 'daniel': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'getting': 0.31; 'lines': 0.31; 'subject:some': 0.31; 'url:python': 0.33; 'becomes': 0.33; "i'd": 0.34; 'etc': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'yield': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'wrong': 0.37; 'starting': 0.37; 'manager': 0.38; 'from:addr:googlemail.com': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'how': 0.40; 'dave': 0.60; 'is.': 0.60; 'solve': 0.60; 'simply': 0.61; "you're": 0.61; 'feeling': 0.68; 'subject:over': 0.84; 'subject:via': 0.84; 'angel': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=qhEwT3B4lM6XQRGxshViGjKDhOA3HLcgwc3OfScc2qw=; b=IpAPWqtcGgJO1fjtyPa0ippFmdIH0/xf5Dt9AlGjHqc2stmeoKJMZdVOJ8PVO6rCxk z/RzlRiMYH9n59kQjDybkNwB1ZskaUusAzCxWFjuh7JgmrdEGMRVrjJ9rXix0nLb72uT cAXdqwDjOpQ00dMn3xgXw2OkpsOou6XrBIXoeMz8kU66cMeQDlOywFdc38AelILH7Y9w lUDYBkekRierlWHYzS45kUdQWGtQgwmyegshB+4R+e0cAQyQkrEtlnmw5nd4ErHSbM2w SLIzJ1qm8wbPn+ZzKL2HWEvQA0nKZx07pvWBxm/lU5n+0CAx/7sAR1A9F3qGRtTqXTX5 fHww== MIME-Version: 1.0 X-Received: by 10.224.39.10 with SMTP id d10mr62201724qae.29.1417189443296; Fri, 28 Nov 2014 07:44:03 -0800 (PST) In-Reply-To: <5478932A.1030509@davea.name> References: <5478932A.1030509@davea.name> Date: Fri, 28 Nov 2014 16:44:03 +0100 Subject: Re: Iterate over text file, discarding some lines via context manager From: "fetchinson ." To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417189445 news.xs4all.nl 2854 [2001:888:2000:d::a6]:45925 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81686 On 11/28/14, Dave Angel wrote: > On 11/28/2014 10: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: >> >> for line in open( 'myfile' ): >> 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 >> >> process( items ) >> >> You see I'd like to ignore lines which are empty, start with a #, or >> are only white space. How would I write a context manager so that the >> above simply becomes >> >> with some_tricky_stuff( 'myfile' ) as items: >> process( items ) >> > > I see what you're getting at, but a context manager is the wrong > paradigm. What you want is a generator. (untested) > > def mygenerator(filename): > with open(filename) as f: > for line in f: > if not line: continue > if line.startswith('#'): continue > items = line.split() > if not items: continue > yield items > > Now your caller simply does: > > for items in mygenerator(filename): > process(items) Great, thanks a lot! Cheers, Daniel > > -- > DaveA > -- > https://mail.python.org/mailman/listinfo/python-list > -- Psss, psss, put it down! - http://www.cafepress.com/putitdown