'string_escape' in python 3

Ian Kelly ian.g.kelly at gmail.com
Fri Apr 6 19:10:17 EDT 2012


On Fri, Apr 6, 2012 at 11:52 AM, Nicholas Cole <nicholas.cole at gmail.com> wrote:
> In Python 2 given the following raw string:
>
>>>> s = r"Hello\x3a this is a test"
>
> the escaping could be removed by use of the following:
>
>>>> s.decode('string_escape')
>
> In Python 3, however, the only way I can see to achieve the same
> result is to convert into a byte stream and then back:
>
>>>> bytes(s, 'utf-8').decode('unicode_escape')
>
>
> This seems very ugly (and slightly 'wrong').  Is there no way to do
> this without using bytes?  Have I missed something?

>>> import codecs
>>> codecs.getdecoder('unicode_escape')(s)[0]
'Hello: this is a test'

Cheers,
Ian



More information about the Python-list mailing list