How to output list of floats to a binary file in Python
See: Python's struct module
import struct
s = struct.pack('f'*len(floats), *floats)
f = open('file','wb')
f.write(s)
f.close()
Alex is absolutely right, it's more efficient to do it this way:
from array import array
output_file = open('file', 'wb')
float_array = array('d', [3.14, 2.7, 0.0, -1.0, 1.1])
float_array.tofile(output_file)
output_file.close()
And then read the array like that:
input_file = open('file', 'rb')
float_array = array('d')
float_array.fromstring(input_file.read())
array.array
objects also have a .fromfile
method which can be used for reading the file, if you know the count of items in advance (e.g. from the file size, or some other mechanism)
The array module in the standard library may be more suitable for this task than the struct module which everybody is suggesting. Performance with 200 MB of data should be substantially better with array.
If you'd like to take at a variety of options, try profiling on your system with something like this
I'm not sure how NumPy will compare performance-wise for your application, but it may be worth investigating.
Using NumPy:
from numpy import array
a = array(floats,'float32')
output_file = open('file', 'wb')
a.tofile(output_file)
output_file.close()
results in a 20 byte file as well.