Here's an example for writing, the reading the file (so you can see you get the right answer modulo some rounding errors).
import csv, os
import struct
test_floats = [1.2, 0.377, 4.001, 5, -3.4]
## write test floats to a new csv file:
path_test_csv = os.path.abspath('data-test/test.csv')
print path_test_csv
test_csv = open(path_test_csv, 'w')
wr = csv.writer(test_csv)
for x in test_floats:
wr.writerow([x])
test_csv.close()
## write test floats as binary
path_test_binary = os.path.abspath('data-test/test.binary')
test_binary = open(path_test_binary, 'w')
for x in test_floats:
binary_data = struct.pack('<f', x)
test_binary.write(binary_data)
test_binary.close()
## read in test binary
binary = open(path_test_binary, 'rb')
binary.seek(0,2) ## seeks to the end of the file (needed for getting number of bytes)
num_bytes = binary.tell() ## how many bytes are in this file is stored as num_bytes
# print num_bytes
binary.seek(0) ## seeks back to beginning of file
i = 0 ## index of bytes we are on
while i < num_bytes:
binary_data = binary.read(4) ## reads in 4 bytes = 8 hex characters = 32-bits
i += 4 ## we seeked ahead 4 bytes by reading them, so now increment index i
unpacked = struct.unpack("<f", binary_data) ## <f denotes little endian float encoding
print tuple(unpacked)[0]