Write bits in file

John Nagle nagle at animats.com
Sun May 18 12:42:58 EDT 2008


Monica Leko wrote:
> Hi
> 
> I have a specific format and I need binary representation.  Does
> Python have some built-in function which will, for instance, represent
> number 15 in exactly 10 bits?

    The "struct" module will let you format Python data as a binary
object of specified format.  But it doesn't support arbitrary bit-width
fields; integers can be 1, 2, 4, or 8 bytes.

    If you have a space problem, you might use the "struct" module to
convert to a reasonably concise binary representation, then use the
"gzip" module to compress the file down further.

					John Nagle



More information about the Python-list mailing list