Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'win32': 0.03; 'broken': 0.04; 'mrab': 0.05; 'output': 0.05; 'binary': 0.07; 'see:': 0.07; 'bytes.': 0.09; 'url:github': 0.09; 'variable,': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'codecs': 0.16; 'skip:< 20': 0.16; 'skip:n 110': 0.16; 'sys.stdout': 0.16; 'url:ch': 0.16; 'win7': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'written': 0.21; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'specify': 0.24; 'tells': 0.24; 'environment': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'this:': 0.26; 'post': 0.26; 'header:In-Reply- To:1': 0.27; 'tried': 0.27; 'skip:p 30': 0.29; 'character': 0.29; 'generally': 0.29; '8bit%:3': 0.30; 'characters': 0.30; 'mode': 0.30; 'skip:& 60': 0.30; 'skip:g 30': 0.30; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; '8bit%:2': 0.31; 'url:python': 0.33; 'maybe': 0.34; 'display': 0.35; 'but': 0.35; 'received:google.com': 0.35; '8bit%:9': 0.36; 'skip:s 60': 0.36; 'url:listinfo': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'operating': 0.37; 'skip:p 20': 0.39; 'url:mail': 0.40; 'how': 0.40; 'skip:\xc2 10': 0.60; 'tell': 0.60; 'length': 0.61; 'answer.': 0.68; 'chinese': 0.74; 'power': 0.76 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=plqVHVoC9ZERTbJ3EVWI2ByRfICoDKPytc2FJFWVvDU=; b=0eSJUKf22a+tVg95aOTqBFDARfD7qqIYT3u4JB+PVK6ZKYCmcg3wfulutlgzPehC8f fNNMDnBM/BlbAW5u/QoaOfCUtIaGakUmINzkqrxLlzHX+I28A4qbipCOfjskrgKD1jy6 ZBg6SSETbr0SmD0wXdZJunqkdgXsghCoTJOWOprkEgNGzgrmfayYe6hSrqSLWCQqH8IX OTasMTL01ndueLutvR+gZjYT8CJg2k9ufLMSxuMB/eFRKFzY1Od6gtPxrYf78ZzA0HzP sf4m73Qdw5bp7vHhoTyFWLhJ7o5d7fQmYX+lUxqidUJdoao/P/knr7YueS6vzdWnFjb7 mdVw== MIME-Version: 1.0 X-Received: by 10.152.3.72 with SMTP id a8mr10146390laa.33.1397099105609; Wed, 09 Apr 2014 20:05:05 -0700 (PDT) In-Reply-To: <534601A1.5040005@mrabarnett.plus.com> References: <534601A1.5040005@mrabarnett.plus.com> Date: Thu, 10 Apr 2014 11:05:05 +0800 Subject: Re: How to display chinese character in 65001 in pytohn? From: length power To: MRAB Content-Type: multipart/alternative; boundary=089e01419f34e7ec6604f6a77b8f Cc: python-list@python.org 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: , Newsgroups: comp.lang.python Message-ID: Lines: 166 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397099107 news.xs4all.nl 2903 [2001:888:2000:d::a6]:39186 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70002 --089e01419f34e7ec6604f6a77b8f Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable i tried this way ,and post it in stackoverflow,please see: maybe it is the best answer. Codepage 65001 is generally broken in binary mode as used by Python 3, since _write calls Win32 WriteFile, which calls WriteConsoleA, which returns the number of characters written instead of the number of bytes. That confuses Python. ANSICON can hook WriteFile to fix this for programs that you specify in an environment variable, but that won't help with input. http://stackoverflow.com/questions/22977409/how-to-display-chinese-characte= r-in-65001-in-pytohn?noredirect=3D1#comment35087732_22977409 2014-04-10 10:27 GMT+08:00 MRAB : > On 2014-04-10 02:54, length power wrote: > >> I am in win7 +python3.3. >> >> import os >> os.system("chcp 936") >> fh=3Dopen("test.ch ","w",encoding=3D"utf-8") >> >> fh.write("=E4=BD=A0") >> fh.close() >> os.system("chcp 65001") >> fh=3Dopen("test.ch ","r",encoding=3D"utf-8").read() >> >> print(fh) >> =C3=84=C3=A3 >> >>> print(fh.encode("utf-8")) >> b'\xe4\xbd\xa0' >> >> How can i display the chinese character `=E4=BD=A0` in 65001? >> >> The "chcp 65001" tells the operating system to use UTF-8, but you also > have to tell Python to output UTF-8. Try this: > > from codecs import getwriter > sys.stdout =3D getwriter('utf-8')(sys.stdout.detach()) > > -- > https://mail.python.org/mailman/listinfo/python-list > --089e01419f34e7ec6604f6a77b8f Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
i tried this way ,and post it in stackoverflow,please see:=
maybe it is the best answer.
Codepage 65001 is generally broken in binary mode as used by = Python 3, since=C2=A0_write=C2=A0calls Win32=C2=A0WriteFile, which calls=C2=A0= WriteConsoleA, which returns the number of characters written instead of the number of = bytes. That confuses Python.=C2=A0AN= SICON=C2=A0can hook=C2=A0<= /span>WriteFile=C2=A0to fix this for programs that you specify in an environment variab= le, but that won't help with=C2=A0input.



2014-04-10 10:27 GMT+08:00 MRAB <python@mrabarnett.plus.com= >:
On 2014-04-10 02:54, length = power wrote:
I am in win7 +python3.3.

=C2=A0 =C2=A0 =C2=A0import os
=C2=A0 =C2=A0 =C2=A0os.system("chcp =C2=A0936")
=C2=A0 =C2=A0 =C2=A0fh=3Dopen("test.ch <http://t= est.ch>","w",encoding=3D"utf-8")
=C2=A0 =C2=A0 =C2=A0fh.write("=E4=BD=A0")
=C2=A0 =C2=A0 =C2=A0fh.close()
=C2=A0 =C2=A0 =C2=A0os.system("chcp 65001")
=C2=A0 =C2=A0 =C2=A0fh=3Dopen("test.ch <http://t= est.ch>","r",encoding=3D"utf-8").rea= d()

=C2=A0 =C2=A0 =C2=A0print(fh)
=C2=A0 =C2=A0 =C2=A0=C3=84=C3=A3
=C2=A0 =C2=A0 =C2=A0>>> print(fh.encode("utf-8"))
=C2=A0 =C2=A0 =C2=A0b'\xe4\xbd\xa0'

How can i display the chinese character =C2=A0`=E4=BD=A0` in 65001?

The "chcp 65001" tells the operating system to use UTF-8, but you= also
have to tell Python to output UTF-8. Try this:

from codecs import getwriter
sys.stdout =3D getwriter('utf-8')(sys.stdout.detach())

--
https://mail.python.org/mailman/listinfo/python-list

--089e01419f34e7ec6604f6a77b8f--