how to avoid leading white spaces
Ian Kelly
ian.g.kelly at gmail.com
Mon Jun 6 12:29:15 EDT 2011
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='')
More information about the Python-list
mailing list