Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; "'',": 0.07; 'removes': 0.07; 'string': 0.09; 'character,': 0.09; 'so?': 0.09; 'trailing': 0.09; 'invalid.': 0.16; 'newline,': 0.16; 'newlines': 0.16; 'subject:Unicode': 0.16; 'textfile': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'input': 0.22; 'tend': 0.24; 'paul': 0.24; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'end,': 0.31; 'perl': 0.31; 'writes:': 0.31; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'ryan': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'to:addr:python- list': 0.38; 'files': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'removing': 0.60; 'worry': 0.60; 'relatively': 0.65; 'line,': 0.68; 'captures': 0.84; 'subject:you': 0.87 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 :content-type; bh=cjmCKIqm0CXqtOc8q4K+Rc4lH/dkZB9xE5YWJ3Cc0R0=; b=xuIZLR994girpzI6ZUweX3XKK/fAciDgi+qATY0/fPmV/JLM4y/a0tLQim8fNHQaxP io5xViS3JhP0cXgM+EjPQltlhEkmFEvCf7CXFED66G7Q3o28+sy8YJConcFMLkOzlykP 8QvYH832C81uMzrBSpEdHr97tOhoKWWv05vX6Bs0ejw+dywDNmEZRL83vLV/fnpE/nV5 DaAcqGTMfHALiyqofg1OcUYS+KY1YTVnlmOllDqPJTZRqvL3OZ/bB5ekKEiLfg2/bbXN MRk1tSDqnokTDupOXpGRw5x3A/qU/TPFn0HQgZHX3xSSx5R6sRtmUQdheT8ocUh+T9DB Er6Q== X-Received: by 10.180.92.69 with SMTP id ck5mr1335269wib.15.1401999569465; Thu, 05 Jun 2014 13:19:29 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <7xioof9li6.fsf@ruckus.brouhaha.com> References: <7xr433z0g3.fsf@ruckus.brouhaha.com> <7xioof9li6.fsf@ruckus.brouhaha.com> From: Ian Kelly Date: Thu, 5 Jun 2014 14:18:49 -0600 Subject: Re: Unicode and Python - how often do you index strings? To: Python Content-Type: text/plain; charset=UTF-8 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: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401999570 news.xs4all.nl 2958 [2001:888:2000:d::a6]:52378 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72756 On Thu, Jun 5, 2014 at 1:58 PM, Paul Rubin wrote: > Ryan Hiebert writes: >> How so? I was using line=line[:-1] for removing the trailing newline, and >> just replaced it with rstrip('\n'). What are you doing differently? > > rstrip removes all the newlines off the end, whether there are zero or > multiple. In perl the difference is chomp vs chop. line=line[:-1] > removes one character, that might or might not be a newline. Given the description that the input string is "a textfile line", if it has multiple newlines then it's invalid. Personally I tend toward rstrip('\r\n') so that I don't have to worry about files with alternative line terminators. If you want to be really picky about removing exactly one line terminator, then this captures all the relatively modern variations: re.sub('\r?\n$|\n?\r$', line, '', count=1)