How do I write raw binary data in Python?
- by Chris B.
I've got a Python program that stores and writes data to a file. The data is raw binary data, stored internally as str. I'm writing it out through a utf-8 codec. However, I get UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 25: character maps to <undefined> in the cp1252.py file.
This looks to me like Python is trying to interpret the data using the default code page. But it doesn't have a default code page. That's why I'm using str, not unicode.
I guess my questions are:
How do I represent raw binary data in memory, in Python?
When I'm writing raw binary data out through a codec, how do I encode/unencode it?