如何获得UnicodeDecodeError发生位置? 我发现材料超过here并试图在下面实施。但我只是得到一个错误NameError: name 'err' is not defined
如何获得UnicodeDecodeError发生的位置?
我已经在互联网上搜索了已经在这里和StackOverflow,但找不到任何提示如何使用它。在python文档中,它说这个特殊的异常有start属性,所以它必须是可能的。
谢谢。
data = buffer + data
try:
data = data.decode("utf-8")
except UnicodeDecodeError:
#identify where did the error occure?
#chunk that piece off -> copy troubled piece into buffer and
#decode the good one -> then go back, receive the next chunk of
#data and concatenate it to the buffer.
buffer = err.data[err.start:]
data = data[0:err.start]
data = data.decode("utf-8")
投票方式关闭如琐碎'因为答案是一个重要的语法细节。可能仍然有用,供将来参考。 –