Storing and parsing packed binary data in files

One other file-related note before we move on: some advanced applications also need to deal with packed binary data, created perhaps by a C language program. Python's standard library includes a tool to help in this domain—the struct module knows how to both compose and parse packed binary data. In a sense, this is another data-conversion tool that interprets strings in files as binary data.

To create a packed binary data file, for example, open it in 'wb' (write binary) mode, and pass struct a format string and some Python objects. The format string used here means pack as a 4-byte integer, a 4-character string, and a 2-byte integer, all in big-endian form (other format codes handle padding bytes, floating-point numbers, and more):

>>> F = open('data.bin', 'wb') # Open binary output file

>>> data = struct.pack('>i4sh', 7, 'spam', 8) # Make packed binary data

Python creates a binary bytes data string, which we write out to the file normally—this one consists mostly of nonprintable characters printed in hexadecimal escapes, and is the same binary file we met earlier. To parse the values out to normal Python objects, we simply read the string back and unpack it using the same format string. Python extracts the values into normal Python objects—integers and a string: >>> F = open('data.bin', 'rb')

>>> data = # Get packed binary data

>>> values = struct.unpack('>i4sh', data) # Convert to Python objects

Binary data files are advanced and somewhat low-level tools that we won't cover in more detail here; for more help, see Chapter 36, consult the Python library manual, or import struct and pass it to the help function interactively. Also note that the binary file-processing modes 'wb' and 'rb' can be used to process a simpler binary file such as an image or audio file as a whole without having to unpack its contents.

Search Engine Manifesto

Search Engine Manifesto

Rank Your Web Pages HIGH in TOP Search Engine Popular Results For Maximum Exposure. Search Engines and How They Work Search Engines are special sites on the Web that are designed to help people find information stored on other sites.

Get My Free Ebook

Post a comment