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


Groups > comp.lang.python > #24797

Re: helping with unicode

Date 2012-07-03 02:21 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: helping with unicode
References <56e3cafd-ec4f-4ae4-ad6c-685f2d991403@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1724.1341278482.4697.python-list@python.org> (permalink)

Show all headers | View raw


On 03/07/2012 01:49, self.python wrote:
> it's a simple source view program.
>
> the codec of the target website is utf-8
> so I read it and print the decoded
>
> --------------------------------------------------------------
> #-*-coding:utf8-*-
> import urllib2
>
> rf=urllib2.urlopen(r"http://gall.dcinside.com/list.php?id=programming")
>
> print rf.read().decode('utf-8')
>
> raw_input()
> ---------------------------------------------------------------
>
> It works fine on python shell
>
> but when I make the file "wrong.py" and run it,
> Error rises.
>
> ----------------------------------------------------------------
> Traceback (most recent call last):
>    File "C:wrong.py", line 8, in <module>
>      print rf.read().decode('utf-8')
> UnicodeEncodeError: 'cp949' codec can't encode character u'u1368' in position 5
> 5122: illegal multibyte sequence
> ---------------------------------------------------------------------
>
> cp949 is the basic codec of sys.stdout and cmd.exe
> but I have no idea why it doesn't works.
> printing without decode('utf-8') works fine on IDLE but on cmd, it print broken characters(Ascii portion is still fine, problem is only about the Korean)
>
> the question may look silly:(
> but I want to know what is the problem or how to print the not broken strings.
>
> thanks for reading.
>
The encoding of your console is 'cp949', so when you try to print the
Unicode string, Python tries to encode it as 'cp949'.

Unfortunately, the character (actually, when talking about Unicode the
correct term is 'codepoint') u'\u1368' cannot be encoded into 'cp949'
because that codepoint does not exist in that encoding, in the same way
that ASCII doesn't have Korean characters.

So what is that codepoint?

 >>> import unicodedata
 >>> unicodedata.name(u'\u1368')
'ETHIOPIC PARAGRAPH SEPARATOR'

Apparently 'cp949', which is for the Korean language, doesn't support
Ethiopic codepoints. Somehow that doesn't surprise me! :-)

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


Thread

helping with unicode "self.python" <howmuchistoday@gmail.com> - 2012-07-02 17:49 -0700
  Re: helping with unicode Andrew Berg <bahamutzero8825@gmail.com> - 2012-07-02 20:14 -0500
  Re: helping with unicode MRAB <python@mrabarnett.plus.com> - 2012-07-03 02:21 +0100
  Re: helping with unicode Terry Reedy <tjreedy@udel.edu> - 2012-07-02 21:39 -0400
  Re: helping with unicode Terry Reedy <tjreedy@udel.edu> - 2012-07-02 21:39 -0400

csiph-web