Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'pycon': 0.01; 'subject:text': 0.05; 'context': 0.07; 'discard': 0.07; 'subject:file': 0.07; 'test,': 0.07; 'verbatim': 0.07; "'#'": 0.09; 'caller': 0.09; 'filter,': 0.09; 'line:': 0.09; 'lines:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'at,': 0.16; 'btw:': 0.16; 'empty,': 0.16; 'generator.': 0.16; 'iteration,': 0.16; 'line.split()': 0.16; 'paradigm.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'basically': 0.19; 'slightly': 0.19; '>>>': 0.22; 'example': 0.22; '(in': 0.22; 'header:User-Agent:1': 0.23; 'space.': 0.24; 'sort': 0.25; "i've": 0.25; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'leave': 0.29; 'am,': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'easier': 0.31; 'lines': 0.31; 'subject:some': 0.31; 'writes:': 0.31; 'open': 0.33; 'becomes': 0.33; "i'd": 0.34; 'could': 0.34; 'etc': 0.35; 'but': 0.35; 'sequence': 0.36; 'yield': 0.36; 'should': 0.36; 'wrong': 0.37; 'operating': 0.37; 'starting': 0.37; 'skip:o 20': 0.38; 'message-id:@gmail.com': 0.38; 'manager': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'dave': 0.60; 'is.': 0.60; 'solve': 0.60; 'simply': 0.61; "you're": 0.61; 'more': 0.64; 'feeling': 0.68; 'batchelder': 0.84; 'clearer': 0.84; 'subject:over': 0.84; 'subject:via': 0.84; 'received:89': 0.85; 'angel': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Akira Li <4kir4.1i@gmail.com> Subject: Re: Iterate over text file, discarding some lines via context manager Date: Sat, 29 Nov 2014 00:33:55 +0300 References: <5478932A.1030509@davea.name> Mime-Version: 1.0 Content-Type: text/plain X-Gmane-NNTP-Posting-Host: 89.169.229.68 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:GDDoNVOjONero3XqnWSwE5j5xkA= 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: 92 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417210523 news.xs4all.nl 2865 [2001:888:2000:d::a6]:59959 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81704 Ned Batchelder writes: > On 11/28/14 10:22 AM, 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) >> >> > > I think it's slightly better to leave the open outside the generator: > > def interesting_lines(f): > for line in f: > line = line.strip() > if line.startswith('#'): > continue > if not line: > continue > yield line > > with open("my_config.ini") as f: > for line in interesting_lines(f): > do_something(line) > > This makes interesting_lines a pure filter, and doesn't care what sort > of sequence of strings it's operating on. This makes it easier to > test, and more flexible. The caller's code is also clearer in my > opinion. > > BTW: this example is taken verbatim from my PyCon presentation on > iteration, it you are interested: > http://nedbatchelder.com/text/iter.html The conditions could be combined in this case: def iter_rows(lines): for line in lines: items = line.split() if items and not items[0].startswith('#'): yield items # space-separated non-emtpy non-comment items with open(filename): for items in iter_rows(file): process(items) -- Akira