Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:text': 0.05; 'context': 0.07; 'discard': 0.07; 'subject:file': 0.07; "'#'": 0.09; 'line:': 0.09; 'python': 0.11; 'empty,': 0.16; 'ignore': 0.16; '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; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'lines': 0.31; 'subject:some': 0.31; 'becomes': 0.33; "i'd": 0.34; 'etc': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'should': 0.36; '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; 'how': 0.40; 'is.': 0.60; 'solve': 0.60; 'simply': 0.61; 'feeling': 0.68; 'subject:over': 0.84; 'subject:via': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=IOFjbW6h8x7MytjeK5UnE0sVGTBL0lMXBFiVcJEjN1s=; b=ENp7v62xb++ZEZVnRQmHnqsXxm6ZFZ7FdBb+IJuLHW6kUoZE/OSysna3hPlvH8eMsS UMkFE3LpPD3+jAtHkqxzVUJAbEm6UbXfFjeEXw0TbghNSBY++aqgSDj4R7nQjywV7z+o vc2lr0EMY8h/WDQCY/Ou16Jfc/VE42rtYeiDc5P8QZFw1YOTvMNEh1l4+ZsWBFZzxAYt +Yc/Fh4h9DLvROAp6RWFe1BaV+Mlto7Qs5uE8D3kEGaKAdQJ2c6mt16PcgIPo4h4xSwF FsYkvDe8wv0bO9HCw2TViacnMRvx1Ug9Th9JoVotIa6U8T+5U+JkyfQ+Mytg8xafftCk 4C8w== MIME-Version: 1.0 X-Received: by 10.140.41.71 with SMTP id y65mr62912001qgy.64.1417187070797; Fri, 28 Nov 2014 07:04:30 -0800 (PST) Date: Fri, 28 Nov 2014 16:04:30 +0100 Subject: 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417187079 news.xs4all.nl 2888 [2001:888:2000:d::a6]:32983 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81683 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 ) Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown