Removing whitespace

Alex Martelli alex at magenta.com
Mon Jul 31 07:15:12 EDT 2000


<pauljolly at my-deja.com> wrote in message news:8m3gt7$h1k$1 at nnrp1.deja.com...
> Dear All,
>
> Firstly, apologies for all the questions! However, extensive searching
> and reading of manuals has not helped me here. How does one remove
> whitespace characters from a string (using the re module)? I have tried
> using the re.compile('\s*') method, followed by the split() function
> but no success. Any help much appreciated with example code is possible.

It's easier to do it with the string module --
string.join(string.split(thetext),'').

But you can use a re for the splitting in case it's handier for some reason:

>>> sp=re.compile(r'\s*')
>>> string.join(sp.split(x),'')

Note the r'\s*' in the argument to re.compile -- you're missing that in the
above quote, so that might perhaps be your problem...?


Alex






More information about the Python-list mailing list