1. 程式人生 > >python 讀入csv 出現utf-8 錯誤

python 讀入csv 出現utf-8 錯誤

在解析csv檔案的時候出現報錯:

'utf-8' codec can't decode byte 0xff in position 0

這說明此csv是binary檔案,你應該將它轉換為utf-8就能被python讀取。

或者參考以下方式讀取(但是這樣不能使用諸如strip(), split()等str的函式,因為讀出來是binary檔案)

原文:

錯誤寫法:

 image_data = tf.gfile.FastGFile(image, 'r').read()

解決:

#讀取圖片(檔案在下)
“`
image_data = tf.gfile.FastGFile(image, ‘rb’).read()


參考:
https://stackoverflow.com/questions/42339876/error-unicodedecodeerror-utf-8-codec-cant-decode-byte-0xff-in-position-0-in
Python tries to convert a byte-array (a bytes which it assumes to be a utf-8-encoded string) to a unicode string (str). This process of course is a decoding according to utf-8 rules. When it
tries this, it encounters a byte sequence which is not allowed in utf-8-encoded strings (namely this 0xff at position 0). Since you did not provide any code we could look at, we only could guess on the rest. From the stack trace we can assume that the triggering action was the reading from a file (contents = open
(path).read()). I propose to recode this in a fashion like this:

with open(path, ‘rb’) as f:
contents = f.read()

“`
==That b in the format specifier in the open() states that the file shall be treated as binary, so contents will remain a bytes. No decoding attempt will happen this way.==