0

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>
1
  • 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. Commented Jun 26, 2017 at 15:42

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.