How do I write raw binary data in Python?

Posted by Chris B. on Stack Overflow See other posts from Stack Overflow or by Chris B.
Published on 2010-04-09T21:58:06Z Indexed on 2010/04/09 22:03 UTC
Read the original article Hit count: 204

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about python

Related posts about codecs