您无法在内置类型上重新定义方法,也无法将errors参数的默认值更改为str.decode().但是,还有其他方法可以实现所需的行为.
更好的方法:定义自己的decode()函数:
def decode(s, encoding="ascii", errors="ignore"):
return s.decode(encoding=encoding, errors=errors)
现在,您将需要调用decode(s)而不是s.decode(),但这不是太糟糕,不是吗?
hack:你不能改变errors参数的默认值,但你可以覆盖默认错误=“strict”的处理程序:
import codecs
def strict_handler(exception):
return u"", exception.end
codecs.register_error("strict", strict_handler)
这将基本上将errors =“strict”的行为更改为标准的“忽略”行为.请注意,这将是一个全局更改,会影响您导入的所有模块.
我建议不要这两种方式.真正的解决方案是让你的编码正确. (我很清楚这并不总是可行的.)