Stripping whitespace

Reedick, Andrew jr9445 at ATT.COM
Thu Jan 24 10:30:33 EST 2008


> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of John Machin
> Sent: Wednesday, January 23, 2008 5:48 PM
> To: python-list at python.org
> Subject: Re: Stripping whitespace
> 
> On Jan 24, 7:57 am, "Reedick, Andrew" <jr9... at ATT.COM> wrote:
> >
> > Why is it that so many Python people are regex adverse?  Use the
> dashed
> > line as a regex.  Convert the dashes to dots.  Wrap the dots in
> > parentheses.  Convert the whitespace chars to '\s'.  Presto!
> Simpler,
> > cleaner code.
> 
> >                 pattern = re.sub(r'-', r'.', line)
> >                 pattern = re.sub(r'\s', r'\\s', pattern)
> >                 pattern = re.sub(r'([.]+)', r'(\1)', pattern)
> 
> Consider this:
>     pattern = ' '.join('(.{%d})' % len(x) for x in line.split())
> 

Good.  But the main drawback to using split+join is that it works if
there is only one whitespace char acting as a column separator
(split+join will compress multiple whitespace characters down to one
char.)  Normally, it's safe to assume a one character separator between
columns, however since the input is supposed to be tab delimited (but
wasn't) and tabs tend to get randomly converted to spaces depending on
which butterfly is flapping its wings at any given instant, keeping the
original whitespace column separators is probably a good idea.



*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622





More information about the Python-list mailing list