Correction: Re: string substitutions
John Machin
sjmachin at lexicon.net
Sun Feb 24 01:28:50 EST 2002
Mike Dean <klaatu at evertek.net> wrote in message news:<mailman.1014504639.21135.python-list at python.org>...
> * Mike Dean <klaatu at evertek.net> [2002-23-02 14:22]:
> > import re
> >
> > # And, to combine the two into one operation:
> > re.sub('(\n+| +)', '\1', mystring)
>
> This last line doesn't work as intended - sorry! (didn't proofread it
> carefully enough :-()
> It won't convert those chars to single characters
> (in fact, it does absolutely nothing except consume processor time)...
Try testing instead of proofreading. Post actual examples of input and
output, like this:
>>> mystring = "aaaa\n\n\n\nbbbbbbb\n\n\nbb\nbbbb"
>>> re.sub('(\n+| +)', '\1', mystring)
'aaaa\x01bbbbbbb\x01bb\x01bbbb'
>>> re.sub('(\n+| +)', r'\1', mystring)
'aaaa\n\n\n\nbbbbbbb\n\n\nbb\nbbbb'
>>>
Your posted code replaces matched substrings with "\x01".
What you *meant* to post does nothing but chew up CPU time.
More information about the Python-list
mailing list