内容简介:代码日志版权声明:翻译自: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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
软件人才管理的艺术
Michael Lopp / 罗小平 / 人民邮电出版社 / 201008 / 35.00元
本书作者具有15年的硅谷人才管理经验,他在博客上发表了大量探讨软件人才的管理之道的文章,深受读者欢迎。本书素材取自他的博客文章,用深入浅出的语言,讲述发人深思的道理,具有很强的现实操作性。 本书分为三大部分:“管理的箭袋”、“过程就是产品”、“你的其他版本”。前两部分分别讲述了人员与产品的管理,第三部分除了讨论管理之外,还讲述了如何有针对性地准备简历和电话面试,来提高自己面试成功的几率。书中......一起来看看 《软件人才管理的艺术》 这本书的介绍吧!