Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Neil Cerutti Newsgroups: comp.lang.python Subject: Re: how to avoid leading white spaces Date: 6 Jun 2011 17:17:28 GMT Organization: Norwich University Lines: 39 Message-ID: <954gd8Fh73U1@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> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: individual.net ZbXXkNkXFDrX7ONhgNwYEg20AB/8JRxKCuAP/5i3VMKj0YgWTK Cancel-Lock: sha1:GY4oXtU7BekhOyaRcuI+5RWs70A= User-Agent: slrn/0.9.9p1/mm/ao (Win32) Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7096 On 2011-06-06, 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 I wrestled with using addition like that, and decided against it. The 7 is a magic number and repeats/hides information. I wanted something like: prefix = "TABLE='" start = line.index(prefix) + len(prefix) But decided I searching for the opening ' was a bit better. -- Neil Cerutti