Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'encoded': 0.07; 'python3': 0.07; 'similar,': 0.09; 'subject:Why': 0.09; 'python': 0.11; 'codec': 0.16; 'codecs': 0.16; 'ordinal': 0.16; 'subject:already': 0.16; 'subject:unicode': 0.16; "skip:' 30": 0.19; 'slightly': 0.19; '>>>': 0.22; 'import': 0.22; 'this?': 0.23; '>>>': 0.24; 'byte': 0.24; 'this:': 0.26; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '"",': 0.31; 'sep': 0.31; "skip:' 40": 0.31; 'subject:that': 0.31; 'file': 0.32; 'linux': 0.33; '(most': 0.33; 'skip:& 30': 0.33; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'subject:?': 0.36; 'skip:& 10': 0.38; 'nov': 0.38; 'to:addr :python-list': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'different.': 0.84; '\xe2\x82\xac': 0.84; '2013,': 0.91; '8bit%:18': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=kHjSzTKKleI7kin2B7N2Tdl+z/FXwCl3Lrmi2Z/ebO0=; b=ri8+6ob1doQjiOLRjHbduEOjR9vc468BsZke7/VWMpx3CV4BE00mYjK1Kp3EXKzOly Vq9enK490TKQyf6+0FP+dMegHhW9PJH4QGRy8k/7sobzegQKqUEitvo1HvW5Ub2PWMEC 6atKrFwtMVbe0TlV8Y5+glPx/on8pwP/QXGihIZsiOWN1qivyV6zDOfvYX5Y18WJ6uUn gOpCQIzNE9WIzWnvYoh9KfZGigytfnwfjDU+x0HJP0DwBicpgfQlAeYDN27BoUQJvOmA 2Bf8hUrsnj7rHZRi+Slqh6tAF3iLO3qJgySizuOLoIPuB5Rj8O3OU68DbytHfd83+xsQ 0R5Q== MIME-Version: 1.0 X-Received: by 10.141.28.3 with SMTP id f3mr9096672qhe.37.1431273188498; Sun, 10 May 2015 08:53:08 -0700 (PDT) Date: Sun, 10 May 2015 17:53:08 +0200 Subject: Why does unicode-escape decode escape symbols that are already escaped? From: "Somelauw ." To: python-list@python.org Content-Type: multipart/alternative; boundary=001a1142340efa25210515bc4171 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431273197 news.xs4all.nl 2941 [2001:888:2000:d::a6]:38763 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90291 --001a1142340efa25210515bc4171 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable In Python 3, decoding "=E2=82=AC" with unicode-escape returns '=C3=A2\x82= =C2=AC' which in my opinion doesn't make sense. The =E2=82=AC already is decoded; if it were encoded it would look like thi= s: '\u20ac'. So why is it doing this? In Python 2 the behaviour is similar, but slightly different. $ python3 -S Python 3.3.3 (default, Nov 27 2013, 17:12:35) [GCC 4.8.2] on linux >>> import codecs >>> codecs.decode('=E2=82=AC', 'unicode-escape') '=C3=A2\x82=C2=AC' >>> codecs.encode('=E2=82=AC', 'unicode-escape') b'\\u20ac' >>> $ python2 -S Python 2.7.5+ (default, Sep 17 2013, 15:31:50) [GCC 4.8.1] on linux2 >>> import codecs >>> codecs.decode('=E2=82=AC', 'unicode-escape') u'\xe2\x82\xac' >>> codecs.encode('=E2=82=AC', 'unicode-escape') Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128) >>> --001a1142340efa25210515bc4171 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
In Python 3, decoding "=E2=82=AC" with unic= ode-escape returns '=C3=A2\x82=C2=AC' which in my opinion doesn'= ;t make sense.
The =E2=82=AC already is decoded; if it w= ere encoded it would look like this: '\u20ac'.
So w= hy is it doing this?

In Python 2 the behaviour is = similar, but slightly different.

$ python3 -S=
Python 3.3.3 (default, Nov 27 2013, 17:12:35)=C2=A0
[G= CC 4.8.2] on linux
>>> import codecs
>>> codecs.decode('=E2=82=AC', 'unicode-escape&#= 39;)
'=C3=A2\x82=C2=AC'
>>> codecs= .encode('=E2=82=AC', 'unicode-escape')
b'\\u2= 0ac'
>>>

$ pytho= n2 -S
Python 2.7.5+ (default, Sep 17 2013, 15:31:50)=C2=A0
<= div>[GCC 4.8.1] on linux2
>>> import codecs
>>> codecs.decode('=E2=82=AC', 'unicode-escape')=
u'\xe2\x82\xac'
>>> codecs= .encode('=E2=82=AC', 'unicode-escape')
Traceback = (most recent call last):
=C2=A0 File "<stdin>", l= ine 1, in <module>
UnicodeDecodeError: 'ascii' code= c can't decode byte 0xe2 in position 0: ordinal not in range(128)
=
>>>


--001a1142340efa25210515bc4171--