string substitutions

Mike Dean klaatu at evertek.net
Sat Feb 23 15:22:11 EST 2002


* Bob Roberts <bobnotbob at byu.edu> [2002-23-02 11:52]:
> What would be a good way to replace every one or more spaces (" ") in
> a string with just one space?  Or replace any number of newlines with
> just one?

This is a perfect job for Python's regular expression (RE) library. For
example:

import re

# Replace all occurances of one or more spaces with a single space
re.sub(' +', ' ', mystring)
# Ditto for newlines
re.sub('\n+', '\n', mystring)
# And, to combine the two into one operation:
re.sub('(\n+| +)', '\1', mystring)

If you perform this operation more than once, precompile your RE with
re.compile, like so:

spnl = re.compile('(\n+| +)')
spnl.sub('\n', mystring)

That will speed up the substitution, quite a bit for more complicated
expressions (don't know how much of a factor there is with a simple
expression like this, but there will be some speed improvement).

HTH,
Mike




More information about the Python-list mailing list