Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: Irregular last line in a text file, was Re: Regular expressions Date: Wed, 4 Nov 2015 10:07:14 +0000 Lines: 18 Message-ID: References: <662g3blobme52hfoududj27err185v2npm@4ax.com> <20151102204237.6a78abdf@bigbox.christie.dr> <56382F33.8050905@gmail.com> <20151103055018.535e3e42@bigbox.christie.dr> <56397dd9$0$1601$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de YmncyCtYWF+NgQPpy9f5sAi35gKdvN8VwL8KtJntpcjA== 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; 'subject:text': 0.04; 'subject:file': 0.07; 'trailing': 0.07; 'cc:addr:python-list': 0.09; 'lines:': 0.09; 'def': 0.13; 'cc:name:python list': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Regular': 0.16; 'subject:expressions': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'yield': 0.27; 'whitespace': 0.29; 'subject:last': 0.30; "d'aprano": 0.33; 'steven': 0.33; 'received:google.com': 0.35; 'received:209.85': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'oscar': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=Iogupz8X64Y7AQhiIlpfghWV90N647PUOedSE7vzXTg=; b=cfixMqI5hRwTu+aqOT+f+8ilT4RxJB2VVt+QzNgmFEMgZ5b+SUl+bgbwE1B0UKE+qa OOcgaHH/1ahPZVx4Znn2SNvuuXn2940Law1n9PW0GHXvm0846YjWByEcjbP79d6A7IBM DhY1MlQ62Gx+UE0WdpeDqzf2x2jDkjkWXKgojvQFLWGT8hFdtNwGd2qZ3YgsoVMinfFx vWm4spJFjUz8IQ40xFx6lblFHoo3sofBU75Yf5GXOzAms2uPhOlN6iuPepu9KLKWAs35 HkJaFXffrn4z2ISG3WjaeLuu68c/8PBZ6hvCEUlSGv2K/gSiUU5pz/IBvTXOGZZy2aTH rsiQ== X-Received: by 10.112.157.36 with SMTP id wj4mr470020lbb.100.1446631654432; Wed, 04 Nov 2015 02:07:34 -0800 (PST) In-Reply-To: <56397dd9$0$1601$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98216 On 4 November 2015 at 03:39, Steven D'Aprano wrote: > > Better would be this: > > def chomp(lines): > for line in lines: > yield line.rstrip() # remove all trailing whitespace > > > with open(...) as f: > for line in chomp(f): ... with open(...) as f: for line in map(str.rstrip, f): ... -- Oscar