Path: csiph.com!usenet.pasdenom.info!gegeweb.org!news.ecp.fr!feeder3.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.84.MISMATCH!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'broken': 0.03; 'interpreter': 0.04; 'encoded': 0.05; 'strings.': 0.07; 'urllib2': 0.07; 'utf-8': 0.07; 'works.': 0.07; 'python': 0.09; 'encode': 0.09; 'encodes': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sys.stdout': 0.09; 'terry': 0.09; 'url:unicode': 0.09; 'encoding': 0.15; "'replace'": 0.16; "'replace')": 0.16; 'cmd,': 0.16; 'codec': 0.16; 'message- id:@dough.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:unicode': 0.16; 'wrote:': 0.17; 'bytes': 0.17; 'jan': 0.18; 'shell': 0.18; 'windows': 0.19; 'import': 0.21; '2.x': 0.22; 'errors': 0.23; 'idea': 0.24; 'command': 0.24; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'skip:# 10': 0.27; 'question': 0.27; '(as': 0.27; 'restrict': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'fine': 0.28; 'run': 0.28; 'prints': 0.29; 'character': 0.29; 'source': 0.29; 'basic': 0.30; 'error': 0.30; 'file': 0.32; 'print': 0.32; 'idle': 0.33; 'traceback': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'thanks': 0.34; 'mapping': 0.35; 'sequence': 0.35; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'characters': 0.36; 'subject:with': 0.36; 'itself': 0.37; 'uses': 0.37; 'why': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'url:public': 0.62; 'prompt': 0.78; 'received:fios.verizon.net': 0.84; 'url:programming': 0.84; 'url:php': 0.86 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: helping with unicode Date: Mon, 02 Jul 2012 21:39:32 -0400 References: <56e3cafd-ec4f-4ae4-ad6c-685f2d991403@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 In-Reply-To: <56e3cafd-ec4f-4ae4-ad6c-685f2d991403@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1341279615 news.xs4all.nl 6942 [2001:888:2000:d::a6]:47527 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24800 On 7/2/2012 8:49 PM, 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 which re-encodes before printing > -------------------------------------------------------------- > #-*-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 Do you mean the Windows Command Prompt 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 > 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. cp949 is a Euro-Korean multibyte encoding whose mapping is given at http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP949.TXT u1368 is not in the mapping. There is no reason the utf-8 site would restrict itself to the cp949 subset. Perhap it prints in the interpreter because 2.x uses errors = 'replace' rather than 'strict' (as in 3.x). Try print rf.read().decode('utf-8').encode('cp949', errors = 'replace') Non-cp949 chars will print as '?'. > printing without decode('utf-8') works fine on IDLE because IDLE encodes to utf-8, and x.decode('utf-8').encode('utf-8') == x > but on cmd, it print broken characters Printing utf-8 encoded bytes as if cp949 encoded bytes is pretty hilariour > > 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. > -- Terry Jan Reedy