How do I read bytes from raw bin files in python because file.read() function ends up in UnicodeDecodeErrors?
To be specific I am reading a.bin file and I getting with this error.
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>
Krunal
79.9k49 gold badges255 silver badges268 bronze badges
-
You've got to open the file in binary mode, otherwise the default text mode will try to decode the file bytes in Python 3.Mark Ransom– Mark Ransom2017年06月26日 15:42:55 +00:00Commented Jun 26, 2017 at 15:42
1 Answer 1
If you opened the file only with open(filename) it is interpreted as text, not as bytes. You should open the file as a bytes file, like this:
f = open(filename, 'b')
And then f.read() will not give that error
answered Jun 26, 2017 at 15:45
Ofer Sadan
12k6 gold badges42 silver badges66 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py