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的字符串。
这正是我正在寻找的 - 非常感谢! (我选择了2) – ws6079