removing duplicate spaces from a string
Richard Brodie
R.Brodie at rl.ac.uk
Mon Aug 6 06:16:16 EDT 2001
"Ron Johnson" <ron.l.johnson at home.com> wrote in message
news:lvsb7.57979$oh1.21803460 at news2.rdc2.tx.home.com...
> Are there any builtins that will allow me to compress the
> duplicate spaces out
You can use regular expressions (in the re module). They can soon get
complex but they don't start a lot easier than this.
$ python
Python 1.5.2 (V007, Mon Jun 4 20:11:16 2001) [DECC] on OpenVMS Alpha
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
portions Copyright 1996-2000 Uwe Zessin
>>> import re
>>> whitespace = re.compile('\s+')
>>> re.sub(whitespace, ' ', 'foo bar snafu')
'foo bar snafu'
> For anyone with VMS experience, I want to emulate the DCL lexical
> function F$EDIT("COMPRESS").
Strictly , you would need an expression [ \t]+ :
"match characters from the set (space or tab) one or more times".
More information about the Python-list
mailing list