Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'kelly': 0.09; 'message- id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; 'valueerror:': 0.09; '~ethan~': 0.09; 'exception': 0.12; 'am,': 0.14; 'wrote:': 0.14; '-1:': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'received:gateway01.websitewelcome.com': 0.16; 'mon,': 0.17; 'header:In-Reply-To:1': 0.21; 'fixed': 0.27; 'pass': 0.27; 'subject:how': 0.29; 'version,': 0.29; 'import': 0.29; 'to:addr :python-list': 0.33; "isn't": 0.33; 'header:User-Agent:1': 0.35; 'try:': 0.35; 'but': 0.38; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:websitewelcome.com': 0.67; 'received:69.56': 0.77; 'readability': 0.84; 'start)': 0.84 Date: Mon, 06 Jun 2011 10:48:15 -0700 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: Python Subject: Re: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> <65164054-f11d-4f8e-a141-31513e70ca04@h9g2000yqk.googlegroups.com> <954cb5F5qbU1@mid.individual.net> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:4663 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 2 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 38 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307381710 news.xs4all.nl 49175 [::ffff:82.94.164.166]:44388 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7098 Ian Kelly wrote: > On Mon, Jun 6, 2011 at 10:08 AM, Neil Cerutti wrote: >> import re >> >> print("re solution") >> with open("data.txt") as f: >> for line in f: >> fixed = re.sub(r"(TABLE='\S+)\s+'", r"\1'", line) >> print(fixed, end='') >> >> print("non-re solution") >> with open("data.txt") as f: >> for line in f: >> i = line.find("TABLE='") >> if i != -1: >> begin = line.index("'", i) + 1 >> end = line.index("'", begin) >> field = line[begin: end].rstrip() >> print(line[:i] + line[i:begin] + field + line[end:], end='') >> else: >> print(line, end='') > > print("non-re solution") > with open("data.txt") as f: > for line in f: > try: > start = line.index("TABLE='") + 7 > end = line.index("'", start) > except ValueError: > pass > else: > line = line[:start] + line[start:end].rstrip() + line[end:] > print(line, end='') I like the readability of this version, but isn't generating an exception on every other line going to kill performance? ~Ethan~