Performing a number of substitutions on a unicode string

Tim Chase python.list at tim.thechases.com
Tue Dec 20 09:54:16 EST 2011


On 12/20/11 08:02, Arnaud Delobelle wrote:
> Hi all,
>
> I've got to escape some unicode text according to the following map:
>
> escape_map = {
>      u'\n': u'\\n',
>      u'\t': u'\\t',
>      u'\r': u'\\r',
>      u'\f': u'\\f',
>      u'\\': u'\\\\'
> }
>
> The simplest solution is to use str.replace:
>
> def escape_text(text):
>      return text.replace('\\', '\\\\').replace('\n',
> '\\n').replace('\t', '\\t').replace('\r', '\\r').replace('\f', '\\f')
>
> But it creates 4 intermediate strings, which is quite inefficient
> (I've got 10s of MB's worth of unicode strings to escape)

You might try

   def escape_text(text):
     return text.encode("string_escape")

as documented at[1]

-tkc


[1]
http://docs.python.org/release/2.5.2/lib/standard-encodings.html#standard-encodings







More information about the Python-list mailing list