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


Groups > comp.lang.python > #70004

Re: How to display chinese character in 65001 in pytohn?

References <CAOmh4638E4WggAMobFn2qkPbDCgEBkdn5eVub9pjNy2icVuOyA@mail.gmail.com> <534601A1.5040005@mrabarnett.plus.com> <CAOmh461hHwFikg9QBNtkd3GPqoU0pmBpjEd2qx=2x-bdv0CtjQ@mail.gmail.com>
Date 2014-04-10 11:19 +0800
Subject Re: How to display chinese character in 65001 in pytohn?
From length power <elearn2014@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.9109.1397099969.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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 <elearn2014@gmail.com>:

> 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 <https://github.com/adoxa/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-character-in-65001-in-pytohn?noredirect=1#comment35087732_22977409
>
>
> 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.
>>>
>>>      import os
>>>      os.system("chcp  936")
>>>      fh=open("test.ch <http://test.ch>","w",encoding="utf-8")
>>>
>>>      fh.write("你")
>>>      fh.close()
>>>      os.system("chcp 65001")
>>>      fh=open("test.ch <http://test.ch>","r",encoding="utf-8").read()
>>>
>>>      print(fh)
>>>      Äã
>>>      >>> print(fh.encode("utf-8"))
>>>      b'\xe4\xbd\xa0'
>>>
>>> How can i display the chinese character  `你` 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 = getwriter('utf-8')(sys.stdout.detach())
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
>

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


Thread

Re: How to display chinese character in 65001 in pytohn? length power <elearn2014@gmail.com> - 2014-04-10 11:19 +0800

csiph-web