内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/39183614/how-system-consoleoutputencoding-inputencoding-with-python
3.为什么这些失败和如何解决?
[system.console]::InputEncoding = [System.Text.Encoding]::UTF8; [system.console]::OutputEncoding = [System.Text.Encoding]::UTF8; chcp; "import sys print(sys.stdout.encoding) print(sys.stdin.encoding) sys.stdout.write(sys.stdin.readline()) " | sc test.py -Encoding utf8; [char]0x0422+[char]0x0415+[char]0x0421+[char]0x0422+"`n" | py -3 test.py
打印:
Active code page: 65001 cp65001 cp1251 п»ї????
您将数据管道传输到 Python 中在那个时候,Python的stdin不再附加到TTY(你的控制台),并且不会猜测编码可能是什么.而是使用默认的系统区域设置;在您的系统上,这是cp1251(基于Windows Latin 1的代码页).
将
PYTHONIOENCODING
environment variable
设置为覆盖:
PYTHONIOENCODING
If this is set before running the interpreter, it overrides the encoding used for stdin/stdout/stderr, in the syntax encodingname:errorhandler
. Both the encodingname
and the :errorhandler
parts are optional and have the same meaning as in str.encode()
.
PowerShell似乎不支持UNIX命令行环境变量的方式;最简单的就是先设置变量:
Set-Item Env:PYTHONIOENCODING "UTF-8"
甚至
Set-Item Env:PYTHONIOENCODING "cp65001"
因为Windows UTF-8代码页显然不是很完美的UTF-8,这取决于Windows版本,而是使用更轻的或不重要的管道重定向.
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/39183614/how-system-consoleoutputencoding-inputencoding-with-python
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Windows API编程范例入门与提高
东方人华 / 清华大学出版社 / 2004-1-1 / 38.00
本书通过大量实用、经典的范例,以Visual Basic为开发平台由浅入深地介绍了Windows API编程的基本方法和大量的实用技巧。本书采用实例带动知识点的形式,使读者快速入门并逐步得到提高。本书每节即是一个实例,操作步骤详尽,所用到的源文件均可在网站下载。读者可以按照操作步骤完成每个实例的制作,并根据自己的喜好进行修改、举一反三。 本书内容翔实,凝结了作者多年的编程经验,既适合......一起来看看 《Windows API编程范例入门与提高》 这本书的介绍吧!