Unpacking a binary file with Python only returns one value

Unpacking a binary file with Python only returns one value

Your code only displays one float because it only reads four bytes.

Try this:

import struct

# Read all of the data
with open(data_ch04.dat, rb) as input_file:
    data = input_file.read()

# Convert to list of floats
format = {:d}f.format(len(data)//4)
data = struct.unpack(format, data)

# Display some of the data
print len(data), entries
print data[0], data[1], data[2], ...

Unpacking a binary file with Python only returns one value

Leave a Reply

Your email address will not be published. Required fields are marked *