提问人:Vibhore Jain 提问时间:6/26/2017 最后编辑:KrunalVibhore Jain 更新时间:6/27/2017 访问量:1061
Python:file.read() 函数错误 - UnicodeDecodeErrors?
Python: file.read() function error - UnicodeDecodeErrors?
问:
如何在 python 中从原始 bin 文件中读取字节,因为 file.read() 函数最终出现在 UnicodeDecodeErrors 中?
具体来说,我正在阅读一个.bin文件,我遇到了这个错误。
File "F:\Codes\Python\ML\Pybrain_test.py", line 27, in <module>
string = img_set.read(784)
File "F:\Programs\Python\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 1440: character maps to <undefined>
答:
2赞
Ofer Sadan
6/26/2017
#1
如果仅打开文件,则将其解释为文本,而不是字节。您应该将文件作为字节文件打开,如下所示:open(filename)
f = open(filename, 'b')
然后不会给出那个错误f.read()
评论