在python3.5中,.decode("utf-8", "backslashreplace")
是处理部分Unicode、部分未知的遗留编码二进制字符串的一个非常好的选项。有效的UTF-8序列将被解码,无效的将作为转义序列保留。例如
>>> print(b'\xc2\xa1\xa1'.decode("utf-8", "backslashreplace"))
¡\xa1
这就失去了b'\xc2\xa1\xa1'
和b'\xc2\xa1\\xa1'
之间的区别,但是如果你是在“给我买点东西吧,不要太浪费,我以后可以用手来修好”的心态,那可能没问题。在
但是,这是Python3.5中的一个新特性。我正在开发的程序也需要支持3.4和2.7。在这些版本中,它抛出一个异常:
^{pr2}$我找到了一个近似值,但不是完全等价的:
>>> print(b'\xc2\xa1\xa1'.decode("latin1")
... .encode("ascii", "backslashreplace").decode("ascii"))
\xc2\xa1\xa1
非常重要的是,行为不依赖于解释器版本。有人能建议一种方法来精确地获得python3.5在2.7和3.4中的行为吗?在
(旧版本的2.x或3.x都不需要工作。猴子修补codecs
完全可以接受。)
您可以编写自己的错误处理程序。下面是我在Python2.7、3.3和3.6上测试的解决方案:
输出:
^{bq}$我尝试了一个更完整的后端口cpython implementation
这将同时处理}:
UnicodeDecodeError
(来自.decode()
)以及来自.encode()
和来自.translate()
的{相关问题 更多 >
编程相关推荐