how to avoid leading white spaces

Ethan Furman ethan at stoneleaf.us
Mon Jun 6 13:48:15 EDT 2011


Ian Kelly wrote:
> On Mon, Jun 6, 2011 at 10:08 AM, Neil Cerutti <neilc at 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
>             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~



More information about the Python-list mailing list