how to avoid leading white spaces
Neil Cerutti
neilc at norwich.edu
Mon Jun 6 12:08:05 EDT 2011
On 2011-06-06, rurpy at yahoo.com <rurpy at yahoo.com> wrote:
> On 06/03/2011 02:49 PM, Neil Cerutti wrote:
> Can you find an example or invent one? I simply don't remember
> such problems coming up, but I admit it's possible.
>
> Sure, the response to the OP of this thread.
Here's a recap, along with two candidate solutions, one based on
your recommendation, and one using str functions and slicing.
(I fixed a specification problem in your original regex, as one
of the lines of data contained a space after the closing ',
making the $ inappropriate)
data.txt:
//ACCDJ EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB,
// UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCDJ '
//ACCT EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB,
// UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCT '
//ACCUM EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB,
// UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM '
//ACCUM1 EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB,
// UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM1 '
^Z
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='')
These two solutions print identical output processing the sample
data. Slight changes in the data would reveal divergence in the
assumptions each solution made.
I agree with you that this is a very tempting candidate for
re.sub, and if it probably would have been my first try as well.
--
Neil Cerutti
More information about the Python-list
mailing list