Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.04; 'things.': 0.05; 'python': 0.09; '(it': 0.09; '8bit%:30': 0.09; 'encode': 0.09; 'to:addr:comp.lang.python': 0.09; 'unicode,': 0.09; 'cc:addr :python-list': 0.10; 'subject:error': 0.11; '(like': 0.15; 'encoding': 0.15; 'languages.': 0.15; '"import': 0.16; "'replace')": 0.16; '(eg.': 0.16; 'benjamin': 0.16; 'codec': 0.16; 'example).': 0.16; 'subject:unicode': 0.16; 'unicode.': 0.16; '\xc3\xa9crit\xc2\xa0:': 0.16; 'wrote:': 0.17; 'unicode': 0.17; '>>>': 0.18; 'windows': 0.19; 'versions': 0.20; 'skip:" 30': 0.20; 'skip:" 40': 0.20; '"",': 0.22; '(on': 0.22; 'claimed': 0.22; "skip:' 40": 0.22; 'programming': 0.23; "i've": 0.23; 'cc:2**1': 0.24; 'command': 0.24; 'host': 0.24; 'machine': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; '(most': 0.27; 'cc:addr:gmail.com': 0.27; 'coding': 0.27; 'andrew': 0.27; "doesn't": 0.28; 'correct': 0.28; 'skip:( 20': 0.28; '(possibly': 0.29; 'character.': 0.29; 'prints': 0.29; 'scheme.': 0.29; 'character': 0.29; 'this.': 0.29; 'worked': 0.30; 'checked': 0.30; 'code': 0.31; 'point': 0.31; 'system,': 0.32; 'file': 0.32; "skip:' 20": 0.32; 'safely': 0.33; 'traceback': 0.33; 'problem': 0.33; 'version': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'so,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'but': 0.36; '12,': 0.36; 'cc:no real name:2**1': 0.36; 'characters': 0.36; "didn't": 0.36; 'skip:p 20': 0.36; 'possible': 0.37; 'correctly': 0.37; 'does': 0.37; 'two': 0.37; 'previous': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'mean': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'nothing': 0.38; 'page': 0.38; 'where': 0.40; 'help': 0.40; 'think': 0.40; 'from:no real name:2**0': 0.60; 'skip:u 10': 0.60; 'stand': 0.61; 'needing': 0.62; 'here': 0.65; '8bit%:74': 0.65; 'wish': 0.70; '(standard': 0.84; 'actually,': 0.84; 'cheating': 0.84; 'confusing': 0.84; 'novembre': 0.84; 'oscar': 0.84; '850': 0.91; '8bit%:56': 0.91 Newsgroups: comp.lang.python Date: Thu, 8 Nov 2012 11:30:37 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=83.79.110.133; posting-account=ung4FAoAAAC46zhHJ0Nsnuox7M5gDvs_ References: <09a3d20b-5871-47f4-9218-df119698e405@m4g2000yqf.googlegroups.com> <509AF3EF.8050108@gmail.com> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 83.79.110.133 MIME-Version: 1.0 Subject: Re: Right solution to unicode error? From: wxjmfauth@gmail.com To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org, wxjmfauth@gmail.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 247 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352403047 news.xs4all.nl 6912 [2001:888:2000:d::a6]:50615 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32975 Le jeudi 8 novembre 2012 19:32:14 UTC+1, Oscar Benjamin a =C3=A9crit=C2=A0: > On 8 November 2012 15:05, wrote: >=20 > > Le jeudi 8 novembre 2012 15:07:23 UTC+1, Oscar Benjamin a =C3=A9crit : >=20 > >> On 8 November 2012 00:44, Oscar Benjamin = wrote: >=20 > >> > On 7 November 2012 23:51, Andrew Berg wr= ote: >=20 > >> >> On 2012.11.07 17:27, Oscar Benjamin wrote: >=20 > >> >=20 > >> >>> Are you using cmd.exe (standard Windows terminal)? If so, it does = not >=20 > >> >>> support unicode >=20 > >> >=20 > >> >> Actually, it does. Code page 65001 is UTF-8. I know that doesn't he= lp >=20 > >> >> the OP since Python versions below 3.3 don't support cp65001, but I >=20 > >> >> think it's important to point out that the Windows command line sys= tem >=20 > >> >> (it is not unique to cmd) does in fact support Unicode. >=20 > >> >=20 > >> > I have tried to use code page 65001 and it didn't work for me even i= f >=20 > >> > I did use a version of Python (possibly 3.3 alpha) that claimed to >=20 > >> > support it. >=20 > >> >=20 > >> I stand corrected. I've just checked and codepage 65001 does work in >=20 > >> cmd.exe (on this machine): >=20 > >> >=20 > >> O:\>chcp 65001 >=20 > >> Active code page: 65001 >=20 > >> >=20 > >> O:\>Q:\tools\Python33\python -c print('abc\u2013def') >=20 > >> abc-def >=20 > >> >=20 > >> O:\>Q:\tools\Python33\python -c print('\u03b1') >=20 > >> =CE=B1 >=20 > >> >=20 > >> It would be a lot better though if it just worked straight away >=20 > >> without me needing to set the code page (like the terminal in every >=20 > >> other OS I use). >=20 > > >=20 > > It *WORKS* straight away. The problem is that >=20 > > people do not wish to use unicode correctly >=20 > > (eg. Mulder's example). >=20 > > Read the point 1) and 4) in my previous post. >=20 > > >=20 > > Unicode and in general the coding of the characters >=20 > > have nothing to do with the os's or programming languages. >=20 >=20 >=20 > I don't know what you mean that it works "straight away". >=20 >=20 >=20 > The default code page on my machine is cp850. >=20 >=20 >=20 > O:\>chcp >=20 > Active code page: 850 >=20 >=20 >=20 > cp850 doesn't understand utf-8. It just prints garbage: >=20 >=20 >=20 > O:\>Q:\tools\Python33\python -c "import sys; >=20 > sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" >=20 > =E2=95=AC=E2=96=92 >=20 >=20 >=20 > Using the correct encoding doesn't help: >=20 >=20 >=20 > O:\>Q:\tools\Python33\python -c "import sys; >=20 > sys.stdout.buffer.write('\u03b1\n'.encode('cp850'))" >=20 > Traceback (most recent call last): >=20 > File "", line 1, in >=20 > File "Q:\tools\Python33\lib\encodings\cp850.py", line 12, in encode >=20 > return codecs.charmap_encode(input,errors,encoding_map) >=20 > UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in >=20 > position 0: character maps to >=20 > >=20 >=20 >=20 > O:\>Q:\tools\Python33\python -c "import sys; >=20 > sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en >=20 > coding))" >=20 > Traceback (most recent call last): >=20 > File "", line 1, in >=20 > File "Q:\tools\Python33\lib\encodings\cp850.py", line 12, in encode >=20 > return codecs.charmap_encode(input,errors,encoding_map) >=20 > UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in >=20 > position 0: character maps to >=20 > >=20 >=20 >=20 > If I want the other characters to work I need to change the code page: >=20 >=20 >=20 > O:\>chcp 65001 >=20 > Active code page: 65001 >=20 >=20 >=20 > O:\>Q:\tools\Python33\python -c "import sys; >=20 > sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))" >=20 > =CE=B1 >=20 >=20 >=20 > O:\>Q:\tools\Python33\python -c "import sys; >=20 > sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en >=20 > coding))" >=20 > =CE=B1 >=20 >=20 >=20 >=20 >=20 > Oscar You are confusing two things. The coding of the characters and the set of the characters (glyphes/graphemes) of a coding scheme. It is always possible to encode safely an unicode, but the target coding may not contain the character. Take a look at the output of this "special" interactive interpreter" where the host coding (sys.stdout.encoding) can be change on the fly. >>> s =3D '=C3=A9l=C3=A9phant\u2013abc=C3=A9=C5=93=E2=82=AC' >>> sys.stdout.encoding '' >>> s '=C3=A9l=C3=A9phant=E2=80=93abc=C3=A9=C5=93=E2=82=AC' >>>=20 >>> sys.stdout.encoding =3D 'cp1252' >>> s.encode('cp1252') '=C3=A9l=C3=A9phant=E2=80=93abc=C3=A9=C5=93=E2=82=AC' >>> sys.stdout.encoding =3D 'cp850' >>> s.encode('cp850') Traceback (most recent call last): File "", line 1, in File "C:\Python32\lib\encodings\cp850.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 8: character maps to >>> # but >>> s.encode('cp850', 'replace') '=C3=A9l=C3=A9phant?abc=C3=A9??' >>>=20 >>> sys.stdout.encoding =3D 'utf-8' >>> s '=C3=83=C2=A9l=C3=83=C2=A9phant=C3=A2=E2=82=AC=E2=80=9Cabc=C3=83=C2=A9=C3= =85=E2=80=9C=C3=A2=E2=80=9A=C2=AC' >>> s.encode('utf-8') '=C3=A9l=C3=A9phant=E2=80=93abc=C3=A9=C5=93=E2=82=AC' >>>=20 >>> sys.stdout.encoding =3D 'utf-16-le' <<<<<<<<< >>> s ' =C3=A9 l =C3=A9 p h a n t =13 a b c =C3=A9 S =C2=AC ' >>> s.encode('utf-16-le') '=C3=A9l=C3=A9phant=E2=80=93abc=C3=A9=C5=93=E2=82=AC' <<<<<<<<<<< some cheating here do to the mail system, it really looks like = this. jmf