Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Irregular last line in a text file, was Re: Regular expressions Date: Tue, 03 Nov 2015 18:44:36 +0100 Organization: None Lines: 25 Message-ID: References: <662g3blobme52hfoududj27err185v2npm@4ax.com> <20151102204237.6a78abdf@bigbox.christie.dr> <56382F33.8050905@gmail.com> <20151103055018.535e3e42@bigbox.christie.dr> <20151103105653.622d5e34@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de X74kSih1L6R2suNj6eKABgXwk6XJv5KzuiZRb0Pj33ww== 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.04; 'subject:file': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'files.': 0.13; 'loops': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Regular': 0.16; 'subject:expressions': 0.16; 'wrote:': 0.16; 'saying': 0.22; 'tim': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'chase': 0.29; 'subject:last': 0.30; 'maybe': 0.33; 'something': 0.35; 'there': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'end': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'valuable': 0.61; 'skip:n 10': 0.62; 'more': 0.63; 'wish': 0.71; 'special': 0.73; 'obvious': 0.76; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9867.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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:98179 Tim Chase wrote: > On 2015-11-03 16:35, Peter Otten wrote: >> I wish there were a way to prohibit such files. Maybe a special >> value >> >> with open(..., newline="normalize") f: >> assert all(line.endswith("\n") for line in f) >> >> to ensure that all lines end with "\n"? > > Or even more valuable to me: > > with open(..., newline="strip") as f: > assert all(not line.endswith(("\n", "\r")) for line in f) > > because I have countless loops that look something like > > with open(...) as f: > for line in f: > line = line.rstrip('\r\n') > process(line) Indeed. It's obvious now you're saying it...