Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'valueerror:': 0.09; 'am,': 0.14; 'wrote:': 0.14; '-1:': 0.16; '\xa0for': 0.16; 'mon,': 0.17; 'header:In-Reply-To:1': 0.21; '\xa0if': 0.23; 'received:209.85.161.46': 0.23; 'received:mail- fx0-f46.google.com': 0.23; 'received:209.85.161': 0.26; 'pass': 0.27; 'message-id:@mail.gmail.com': 0.28; 'subject:how': 0.29; 'import': 0.29; 'to:addr:python-list': 0.33; 'try:': 0.35; '8bit%:8': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'subject:: ': 0.38; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'start)': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=y6y8/mzQZyXx/kjOGTMwfhu1vUw3QV9T3IgI3+ZAAXc=; b=S+Asc9DiCxW2mL8+gOaQi6k4s+JprVH8tDg3caRFN7bKMFNouEN5oAw8ZWRWO/OIqq vyDf5kQK4IG3Tipy0w+tnMpTqHPxZdAjaTSkfIYhKaa+B7c6clMFjBP2yTZ7g0n0q/QG +E8VrFX18+KSx2MOmJT/BOtwMComELc86x8fE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=a0uPRkgjwjF+yFqQUx4RpjBaetcaXEoR80YsouT7bCDh/jYbjUNSsrj1heoRLHsH91 RIt4gSzYSzpY1Xq6gzgjgEO0mDJdHIghfOJLJTJFYTtu61DkssTEZHBrFPAFrt/JZFD6 MBjb3LvVavdXCts9vHf5GCodTj7tMOG4r4ow8= MIME-Version: 1.0 In-Reply-To: <954cb5F5qbU1@mid.individual.net> 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> From: Ian Kelly Date: Mon, 6 Jun 2011 10:29:15 -0600 Subject: Re: how to avoid leading white spaces To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 33 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307377787 news.xs4all.nl 49174 [::ffff:82.94.164.166]:51186 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7093 On Mon, Jun 6, 2011 at 10:08 AM, Neil Cerutti wrote: > import re > > print("re solution") > with open("data.txt") as f: > =A0 =A0for line in f: > =A0 =A0 =A0 =A0fixed =3D re.sub(r"(TABLE=3D'\S+)\s+'", r"\1'", line) > =A0 =A0 =A0 =A0print(fixed, end=3D'') > > print("non-re solution") > with open("data.txt") as f: > =A0 =A0for line in f: > =A0 =A0 =A0 =A0i =3D line.find("TABLE=3D'") > =A0 =A0 =A0 =A0if i !=3D -1: > =A0 =A0 =A0 =A0 =A0 =A0begin =3D line.index("'", i) + 1 > =A0 =A0 =A0 =A0 =A0 =A0end =3D line.index("'", begin) > =A0 =A0 =A0 =A0 =A0 =A0field =3D line[begin: end].rstrip() > =A0 =A0 =A0 =A0 =A0 =A0print(line[:i] + line[i:begin] + field + line[end:= ], end=3D'') > =A0 =A0 =A0 =A0else: > =A0 =A0 =A0 =A0 =A0 =A0print(line, end=3D'') print("non-re solution") with open("data.txt") as f: for line in f: try: start =3D line.index("TABLE=3D'") + 7 end =3D line.index("'", start) except ValueError: pass else: line =3D line[:start] + line[start:end].rstrip() + line[end:] print(line, end=3D'')