Why does unicode-escape decode escape symbols that are already escaped?

Somelauw . somelauw at gmail.com
Sun May 10 19:45:34 EDT 2015


2015-05-10 20:39 GMT+02:00 Albert-Jan Roskam <fomcl at yahoo.com>:

>
> Hi,
>
> I only have Python 2 on my phone, but I am suprised that you (and are able
> to) decode unicode strings. What result do you get when you do the
> following in Python 3:
>
> Python 2.7.2 (default, Oct 25 2014, 20:52:15)
> [GCC 4.9 20140827 (prerelease)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import codecs
> >>> codecs.decode(b'€', 'unicode-escape')
> u'\xe2\x82\xac'
> >>> codecs.encode(u'€', 'unicode-escape')
> '\\xe2\\x82\\xac'
> >>>
>
>
Python 3.3.3 (default, Nov 27 2013, 17:12:35)
[GCC 4.8.2] on linux
>>> import codecs
>>> codecs.decode(b'€', 'unicode-escape')
  File "<stdin>", line 1
SyntaxError: bytes can only contain ASCII literal characters.
>>> codecs.encode(u'€', 'unicode-escape')
b'\\u20ac'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150511/e970cf06/attachment.html>


More information about the Python-list mailing list