Annie C
Annie C

Reputation: 804

parse binary format with python

I have a binary file with the following header: 4 byte string, 1 byte number then 4 byte uint32 number.

Do I understand this correctly? The sbet_data[0:3] is the string, sbet_data[4:5] is the 1 byte number, then how long is the 4 byte uint32 number? Where can I find a good chart for corresponding byte size vs format, for example I would also like to know the size for 8 byte (uint64).

sbet_file = open('abc.dat')
sbet_data = sbet_file.read()

s = struct.Struct('4s b I')
unpacked_data = s.unpack(sbet_data[0:12])

Upvotes: 1

Views: 2992

Answers (2)

Mike Müller
Mike Müller

Reputation: 85612

You need to open your file in binary mode and read only 12 bytes from your file:

import struct

with open('abc.dat', 'rb') as fobj:
    byte_string, n1, n4 = struct.unpack('4sbI', fobj.read(12)) 

You will get a byte string. Assuming it is ASCII, you can decode like this:

my_string = byte_string.decode('ascii')

The documentation of the struct contains tables of format strings. According to one of these tables a uint64 would be L.

Upvotes: 4

Xavier Merino
Xavier Merino

Reputation: 669

I believe you are trying to extract information from the binary. Well this will work

import struct 
import numpy as np

buffer = np.random.bytes(12)
s = struct.Struct('4sbI')
unpacked_data = s.unpack(buffer)
print unpacked_data[0], unpacked_data[1], unpacked_data[2]

In this case unpacked_data[0] will be the string, unpacked_data[1] will be the 1 byte number and the 4 byte integer will be unpacked_data[2].

Keep in mind you can also use numpy to unpack the data using the np.ndarray constructor if you would like to improve the speed.

Upvotes: 2

Related Questions