Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32975

Re: Right solution to unicode error?

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 <wxjmfauth@gmail.com>
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 <mailman.3457.1352399533.27098.python-list@python.org>
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> <CAHVvXxT8SpK24HtsS8XTfs0CHTBofh3=deYw5xY1nq5ctATjrg@mail.gmail.com> <509AF3EF.8050108@gmail.com> <CAHVvXxSg0XWjZmA1pnEvBB_b2T9pS91YdjhF2duZHmBuLbXNAw@mail.gmail.com> <mailman.3436.1352383603.27098.python-list@python.org> <65910cea-f145-409c-a579-9f0cda499546@googlegroups.com> <mailman.3457.1352399533.27098.python-list@python.org>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.3461.1352403047.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


Le jeudi 8 novembre 2012 19:32:14 UTC+1, Oscar Benjamin a écrit :
> On 8 November 2012 15:05,  <wxjmfauth@gmail.com> wrote:
> 
> > Le jeudi 8 novembre 2012 15:07:23 UTC+1, Oscar Benjamin a écrit :
> 
> >> On 8 November 2012 00:44, Oscar Benjamin <oscar.j.benjamin@gmail.com> wrote:
> 
> >> > On 7 November 2012 23:51, Andrew Berg <bahamutzero8825@gmail.com> wrote:
> 
> >> >> On 2012.11.07 17:27, Oscar Benjamin wrote:
> 
> >>
> 
> >> >>> Are you using cmd.exe (standard Windows terminal)? If so, it does not
> 
> >> >>> support unicode
> 
> >>
> 
> >> >> Actually, it does. Code page 65001 is UTF-8. I know that doesn't help
> 
> >> >> the OP since Python versions below 3.3 don't support cp65001, but I
> 
> >> >> think it's important to point out that the Windows command line system
> 
> >> >> (it is not unique to cmd) does in fact support Unicode.
> 
> >>
> 
> >> > I have tried to use code page 65001 and it didn't work for me even if
> 
> >> > I did use a version of Python (possibly 3.3 alpha) that claimed to
> 
> >> > support it.
> 
> >>
> 
> >> I stand corrected. I've just checked and codepage 65001 does work in
> 
> >> cmd.exe (on this machine):
> 
> >>
> 
> >> O:\>chcp 65001
> 
> >> Active code page: 65001
> 
> >>
> 
> >> O:\>Q:\tools\Python33\python -c print('abc\u2013def')
> 
> >> abc-def
> 
> >>
> 
> >> O:\>Q:\tools\Python33\python -c print('\u03b1')
> 
> >> α
> 
> >>
> 
> >> It would be a lot better though if it just worked straight away
> 
> >> without me needing to set the code page (like the terminal in every
> 
> >> other OS I use).
> 
> >
> 
> > It *WORKS* straight away. The problem is that
> 
> > people do not wish to use unicode correctly
> 
> > (eg. Mulder's example).
> 
> > Read the point 1) and 4) in my previous post.
> 
> >
> 
> > Unicode and in general the coding of the characters
> 
> > have nothing to do with the os's or programming languages.
> 
> 
> 
> I don't know what you mean that it works "straight away".
> 
> 
> 
> The default code page on my machine is cp850.
> 
> 
> 
> O:\>chcp
> 
> Active code page: 850
> 
> 
> 
> cp850 doesn't understand utf-8. It just prints garbage:
> 
> 
> 
> O:\>Q:\tools\Python33\python -c "import sys;
> 
> sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))"
> 
> ╬▒
> 
> 
> 
> Using the correct encoding doesn't help:
> 
> 
> 
> O:\>Q:\tools\Python33\python -c "import sys;
> 
> sys.stdout.buffer.write('\u03b1\n'.encode('cp850'))"
> 
> Traceback (most recent call last):
> 
>   File "<string>", line 1, in <module>
> 
>   File "Q:\tools\Python33\lib\encodings\cp850.py", line 12, in encode
> 
>     return codecs.charmap_encode(input,errors,encoding_map)
> 
> UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in
> 
> position 0: character maps to
> 
>  <undefined>
> 
> 
> 
> O:\>Q:\tools\Python33\python -c "import sys;
> 
> sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en
> 
> coding))"
> 
> Traceback (most recent call last):
> 
>   File "<string>", line 1, in <module>
> 
>   File "Q:\tools\Python33\lib\encodings\cp850.py", line 12, in encode
> 
>     return codecs.charmap_encode(input,errors,encoding_map)
> 
> UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in
> 
> position 0: character maps to
> 
>  <undefined>
> 
> 
> 
> If I want the other characters to work I need to change the code page:
> 
> 
> 
> O:\>chcp 65001
> 
> Active code page: 65001
> 
> 
> 
> O:\>Q:\tools\Python33\python -c "import sys;
> 
> sys.stdout.buffer.write('\u03b1\n'.encode('utf-8'))"
> 
> α
> 
> 
> 
> O:\>Q:\tools\Python33\python -c "import sys;
> 
> sys.stdout.buffer.write('\u03b1\n'.encode(sys.stdout.en
> 
> coding))"
> 
> α
> 
> 
> 
> 
> 
> 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 = 'éléphant\u2013abc需'
>>> sys.stdout.encoding
'<unicode>'
>>> s
'éléphant–abc需'
>>> 
>>> sys.stdout.encoding = 'cp1252'
>>> s.encode('cp1252')
'éléphant–abc需'
>>> sys.stdout.encoding = 'cp850'
>>> s.encode('cp850')
Traceback (most recent call last):
  File "<eta last command>", line 1, in <module>
  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 <undefined>
>>> # but
>>> s.encode('cp850', 'replace')
'éléphant?abcé??'
>>> 
>>> sys.stdout.encoding = 'utf-8'
>>> s
'éléphant–abc需'
>>> s.encode('utf-8')
'éléphant–abc需'
>>> 
>>> sys.stdout.encoding = 'utf-16-le'  <<<<<<<<<
>>> s
' é l é p h a n t  a b c é S ¬ '
>>> s.encode('utf-16-le')
'éléphant–abc需'

<<<<<<<<<<< some cheating here do to the mail system, it really looks like this.

jmf

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Right solution to unicode error? Anders <aschneiderman@asha.org> - 2012-11-07 14:17 -0800
  RE: Right solution to unicode error? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-07 23:07 +0000
  Re: Right solution to unicode error? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-07 23:27 +0000
  Re: Right solution to unicode error? Andrew Berg <bahamutzero8825@gmail.com> - 2012-11-07 17:51 -0600
  Re: Right solution to unicode error? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-07 23:53 +0000
    Re: Right solution to unicode error? Hans Mulder <hansmu@xs4all.nl> - 2012-11-08 12:40 +0100
  Re: Right solution to unicode error? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-08 00:44 +0000
  Re: Right solution to unicode error? wxjmfauth@gmail.com - 2012-11-08 03:01 -0800
  RE: Right solution to unicode error? Anders Schneiderman <ASchneiderman@asha.org> - 2012-11-08 09:00 -0500
  Re: Right solution to unicode error? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-08 14:06 +0000
    Re: Right solution to unicode error? wxjmfauth@gmail.com - 2012-11-08 07:05 -0800
      Re: Right solution to unicode error? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-08 18:32 +0000
        Re: Right solution to unicode error? wxjmfauth@gmail.com - 2012-11-08 11:30 -0800
        Re: Right solution to unicode error? wxjmfauth@gmail.com - 2012-11-08 11:30 -0800
      Re: Right solution to unicode error? Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-08 11:48 -0700
        Re: Right solution to unicode error? wxjmfauth@gmail.com - 2012-11-08 11:54 -0800
          Re: Right solution to unicode error? Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-08 13:41 -0700
            Re: Right solution to unicode error? wxjmfauth@gmail.com - 2012-11-09 02:06 -0800
          RE: Right solution to unicode error? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-08 20:54 +0000
          Re: Right solution to unicode error? Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-08 14:07 -0700
          Re: Right solution to unicode error? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-08 21:37 +0000
        Re: Right solution to unicode error? wxjmfauth@gmail.com - 2012-11-08 11:54 -0800
  Re: Right solution to unicode error? Andrew Berg <bahamutzero8825@gmail.com> - 2012-11-08 21:30 -0600

csiph-web