如何用空格替换不能用utf8解码的字符?在
# -*- coding: utf-8 -*-
print unicode('\x97', errors='ignore') # print out nothing
print unicode('ABC\x97abc', errors='ignore') # print out ABCabc
如何打印出ABC abc
而不是{\x97
只是一个示例字符。无法解码的字符是未知输入。在
errors='ignore'
,它将不打印任何内容。在errors='replace'
,它将用一些特殊字符替换该字符。在
您可以使用
try-except
语句来处理UnicodeDecodeError
:然后使用
^{pr2}$str.join
方法连接字符串:演示:
看看
codecs.register_error
。可以使用它注册自定义错误处理程序{a1}
相关问题 更多 >
编程相关推荐