Performing a number of substitutions on a unicode string

Arnaud Delobelle arnodel at gmail.com
Tue Dec 20 12:04:48 EST 2011


On 20 December 2011 15:35, Peter Otten <__peter__ at web.de> wrote:

>>>> escape_map = {
> ...     u'\n': u'\\n',
> ...     u'\t': u'\\t',
> ...     u'\r': u'\\r',
> ...     u'\f': u'\\f',
> ...     u'\\': u'\\\\'
> ... }
>>>> escape_map = dict((ord(k), v) for k, v in escape_map.items())
>>>> print u"the quick\n brown\tfox
> jumps\\over\\the\\lazy\\dog".translate(escape_map)
> the quick\n brown\tfox jumps\\over\\the\\lazy\\dog

Thanks.  I think I've got a blind spot about unicode.translate.
Actually, I know why: it's not properly documented where I look [1].

-- 
Arnaud

[1] http://docs.python.org/library/stdtypes.html#str.translate



More information about the Python-list mailing list