管道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)
是否有更好的方法使管道工作?