python3更改了unicode行为以拒绝代理项对,而python2没有。
有个问题here
但它并没有提供一个解决方案,如何删除在python2中的代理对或如何做代理逃逸。
python3示例:

>>> a = b'\xed\xa0\xbd\xe4\xbd\xa0\xe5\xa5\xbd'
>>> a.decode('utf-8', 'surrogateescape')
'\udced\udca0\udcbd你好'
>>> a.decode('utf-8', 'ignore')
'你好'

这里的'\xed\xa0\xbd'不是正确的utf-8字符。我想无视他们或者逃避他们。
有没有可能在蟒蛇中做同样的事情?

最佳答案

没有内置的解决方案,但在未来的python中有一个代理网关转义的实现:
https://github.com/PythonCharmers/python-future
from future.utils.surrogateescape import register_surrogateescape添加到导入。然后调用方法register_surrogateescape(),然后可以在errors='surrogateescape'encode中使用decode错误处理程序。
可以找到一个例子here

07-26 00:46