管道Python程序的输出时,Python解释器会对编码感到困惑,并将其设置为None.这意味着这样的程序:
# -*- coding: utf-8 -*-
print u"åäö"
Run Code Online (Sandbox Code Playgroud)
正常运行时会正常工作,但失败时:
UnicodeEncodeError:'ascii'编解码器无法对位置0中的字符u'\ xa0'进行编码:序数不在范围内(128)
当在管道序列中使用时.
在配管时使这项工作的最佳方法是什么?我可以告诉它使用shell/filesystem /无论使用什么编码吗?
到目前为止我看到的建议是直接修改你的site.py,或者使用这个hack对defaultencoding进行硬编码:
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
print u"åäö"
Run Code Online (Sandbox Code Playgroud)
是否有更好的方法使管道工作?
在Python 2中设置默认输出编码是一个众所周知的习惯用法:
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
Run Code Online (Sandbox Code Playgroud)
这将sys.stdout对象包装在编解码器编写器中,该编解码器编写器以UTF-8编码输出.
但是,这种技术在Python 3中不起作用,因为sys.stdout.write()期望a str,但是编码的结果是bytes,并且当codecs尝试将编码的字节写入原始时发生错误sys.stdout.
在Python 3中执行此操作的正确方法是什么?
假设一个人不能使用print(从而享受自动编码检测的好处).所以这让我们失望了sys.stdout.但是,如果不做任何明智的编码sys.stdout是如此愚蠢.
现在,您可以阅读Python维基页面PrintFails并尝试以下代码:
$ python -c 'import sys, codecs, locale; print str(sys.stdout.encoding); \
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout);
Run Code Online (Sandbox Code Playgroud)
然而,这也不起作用(至少在Mac上).太明白为什么:
>>> import locale
>>> locale.getpreferredencoding()
'mac-roman'
>>> sys.stdout.encoding
'UTF-8'
Run Code Online (Sandbox Code Playgroud)
(UTF-8是终端理解的).
所以将上面的代码更改为:
$ python -c 'import sys, codecs, locale; print str(sys.stdout.encoding); \
sys.stdout = codecs.getwriter(sys.stdout.encoding)(sys.stdout);
Run Code Online (Sandbox Code Playgroud)
现在,unicode字符串被正确发送到sys.stdout终端上并在终端上正确打印(sys.stdout连接到终端).
这是编写unicode字符串的正确方法sys.stdout还是我应该做的其他事情?
编辑:有时 - 比如说,当输出到less- 时sys.stdout.encoding将是None.在这种情况下,上面的代码将失败.