> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:python-
> [EMAIL PROTECTED] On Behalf Of John Machin
> Sent: Wednesday, January 23, 2008 5:48 PM
> To: python-list@python.org
> Subject: Re: Stripping whitespace
> 
> On Jan 24, 7:57 am, "Reedick, Andrew" <[EMAIL PROTECTED]> 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


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to