Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.roellig-ltd.de!open-news-network.org!cyclone03.ams2.highwinds-media.com!news.highwinds-media.com!voer-me.highwinds-media.com!eweka.nl!lightspeed.eweka.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4.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; 'python.': 0.02; 'win32': 0.03; 'broken': 0.04; 'mrab': 0.05; 'output': 0.05; 'binary': 0.07; 'see:': 0.07; 'bytes.': 0.09; 'happen.': 0.09; 'url:github': 0.09; 'variable,': 0.09; 'subject:How': 0.10; 'cc:addr:python- list': 0.11; 'python': 0.11; 'cmd': 0.16; 'codecs': 0.16; 'skip:< 20': 0.16; 'skip:n 110': 0.16; 'skip:n 50': 0.16; 'sys.stdout': 0.16; 'url:ch': 0.16; 'win7': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'written': 0.21; '>>>': 0.22; 'input': 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; '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; 'problem': 0.35; "can't": 0.35; 'display': 0.35; 'but': 0.35; 'received:google.com': 0.35; '8bit%:9': 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=sHLBE/Fv+4iRClsnrN/y2JIpnfxHTA4GVwnE8S9YxGY=; b=kcuf4NTcGkqcAFybkDwTplSic+w9Wm6Tv0UBro7WobHEMBGVsxmkmOFKWHuMxYyKSO WcE1pVNsLJKNM5SjyTuma23UqFeO3Vcbyt5wWFdQohjlCXLU0H0IoCl4wYfQ/cpn+oAA ll8w8VWzanKYHLlaSerWRHyQg4fVCmK8CMIh04ME7sggbwi6zsGC2sw6Rpgpv1Uf2u3L HpgixbaAT2IvY0AI9uAHdWcKnxfBDkEZQR9tjDxZwasRENi/hQ3aEIH8mZ65rqg8QiQ/ DAq0abh3L8l26jDt+G+c45OJYmT63FznbxyFRuHgMJ55XFJYx79XYdVUNHO0KUKArY9l 2tkA== MIME-Version: 1.0 X-Received: by 10.152.36.73 with SMTP id o9mr10256490laj.30.1397099967472; Wed, 09 Apr 2014 20:19:27 -0700 (PDT) In-Reply-To: References: <534601A1.5040005@mrabarnett.plus.com> Date: Thu, 10 Apr 2014 11:19:27 +0800 Subject: Re: How to display chinese character in 65001 in pytohn? From: length power To: MRAB Content-Type: multipart/alternative; boundary=089e0160adf846e99304f6a7af02 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: 201 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397099969 news.xs4all.nl 2901 [2001:888:2000:d::a6]:46204 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 14677 X-Received-Body-CRC: 1980956839 Xref: csiph.com comp.lang.python:70004 --089e0160adf846e99304f6a7af02 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable problem solved.if you enter python by python(command line),the problem can't be solved.if you enter python by cmd ,and input python,no problem happen. 2014-04-10 11:05 GMT+08:00 length power : > 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-charac= ter-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 >> > > --089e0160adf846e99304f6a7af02 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
problem solved.if y= ou enter python by python(command line),the problem can't be solved.if = you enter python by cmd ,and input=C2=A0python,no = problem happen.=C2=A0


2014-04= -10 11:05 GMT+08:00 length power <elearn2014@gmail.com>:<= br>
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=A0ANSICON=C2= =A0can hook=C2=A0WriteFile=C2=A0to fix this for programs that you specify in an = environment variable, but that won't help with=C2=A0= input.



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


--089e0160adf846e99304f6a7af02--