How to Store Binary data in a *.py module

Steven Adams sadams123 at optushome.com.au
Wed Jun 19 16:05:36 EDT 2002


> I know a Python string can contain an arbitrary set of bytes, but I
> don't know how to express binary data as a literal. If I were to try to
> use r" ..." and I happened to have a byte that matched the ", it would
> be mis-parsed. I'm imaging that UUencoding or something might solve my
> problem, but I don't know anything about that.
>
> Anyone have an idea??

I've stored GIFs using base64 encoding via the base64 module and it seemed
to work fine.

in the interpreter

read in the file in binary mode
base64 encode it
copy and paste the encoded string to your module
decode it when you want use it as binary data again

Steven





More information about the Python-list mailing list