efficient mega-replacements

Jonathan Hogg jonathan at onegoodidea.com
Thu Jan 24 12:04:54 EST 2002


On 23/1/2002 19:36, in article
mailman.1011813577.11632.python-list at python.org, "Clark C . Evans"
<cce at clarkevans.com> wrote:

> Is there an efficient way to do multiple replacements?
> 
>           val = val.replace("'","''")\
>                    .replace("\\","\\\\\\\\\\\\\\\\")\
>                    .replace(chr(13)+chr(10),"\\\\\\\\n")\
>                    .replace(chr(10),"\\\\\\\\n")\
>                    .replace(chr(13),"\\\\\\\\n")\
>                    .replace('"','\\\\"')

In case you hadn't realised it I thought I'd note that using "regexp"
strings will save a lot of the stramash of backslashes in your code, as
follows:

            val = val.replace("'","''")\
                     .replace(r"\",r"\\\\\\\\")\
                     .replace(chr(13)+chr(10),r"\\\\n")\
                     .replace(chr(10),r"\\\\n")\
                     .replace(chr(13),r"\\\\n")\
                     .replace('"',r'\\"')

The r'' form of string literals doesn't interpret backslashes.
 
> Or is this the best way to do it?

For what definition of "best"? If you want to be concise then this is fine.
It's also fairly readable. If you want it to be fast, then it does a few
more passes over the string that necessary - if the strings are huge then
this might be a problem.

If you will need to do quite a few more replacments then I'd say write
something nicer, otherwise leave it alone. The ugliest part of the code is
all the string literals, and that's not going to get any prettier with any
implementation.

[I'd probably add more whitespace though.]

Jonathan




More information about the Python-list mailing list