如何獲得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")
投票方式關閉如瑣碎'因爲答案是一個重要的語法細節。可能仍然有用,供將來參考。 –