On 2011-06-06, Ian Kelly <ian.g.ke...@gmail.com> wrote: > On Mon, Jun 6, 2011 at 10:08 AM, Neil Cerutti <ne...@norwich.edu> 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 -- http://mail.python.org/mailman/listinfo/python-list