2017-01-13 110 views
0

我见过几个与此相关的帖子,但没有明确的答案。假设我想在仅支持ASCII的终端中打印字符串s=u'\xe9\xe1'(例如,LC_ALL=C; python3)。有什么办法来配置以下为默认行为:Python的默认字符编码处理

import sys 
s = u'\xe9\xe1' 
s = s.encode(sys.stdout.encoding, 'replace').decode(sys.stdout.encoding) 
print(s) 

即,我想串打印的东西 - 即使是垃圾 - 而不是抛出一个异常(UnicodeEncodeError)。我正在使用python3.5。

我想避免编写所有可能包含UTF-8的字符串。

回答

1

你可以做的三两件事之一:

  • 调整错误处理程序stdoutstderrPYTHONIOENCODING environment variable

    export PYTHONIOENCODING=:replace 
    

    注意:;我没有指定编解码器,只有错误处理程序。

  • 更换stdoutTextIOWrapper,设置不同的错误处理程序:

    import sys 
    import io 
    
    sys.stdout = io.TextIOWrapper(
        sys.stdout.buffer, encoding=sys.stdout.encoding, 
        errors='replace', 
        line_buffering=sys.stdout.line_buffering) 
    
  • 创建围绕sys.stdout.buffer单独TextIOWrapper实例,并把它传递为file参数打印时:

    import sys 
    import io 
    
    replacing_stdout = io.TextIOWrapper(
        sys.stdout.buffer, encoding=sys.stdout.encoding, 
        errors='replace', 
        line_buffering=sys.stdout.line_buffering) 
    
    print(s, file=replacing_stdout) 
    
+0

这正是我正在寻找的 - 非常感谢! (我选择了2) – ws6079