pythonnumpynumpy-ndarray

Storing numpy array in raw binary file


How to store a 2D numpy ndarray in raw binary format? It should become a raw array of float32 values, in row-major order, no padding, without any headers.

According to the documentations, ndarray.tofile() can store it as binary or textual, but the format argument is a string to textual formatting. And np.save() saves it in .npy format.


Solution

  • with open('out', 'wb') as f: f.write(arr.tobytes())
    

    should do.

    You may have to add a astype(np.float32) if arr is not already a float32 array.

    Likewise, if the array is not already in "row major order", you may have to add a .T somewhere.

    And of course (but I take you know that very well, if you want to dump binary representation) you need to be aware of little/big endian order.

    EDIT: Since you've mentioned tofile', I looked at it. Indeed, that the same thing. Just don't pass any format (otherwise, indeed, it is a text file)

    with open('out', 'wb') as f: arr.tofile(f)
    

    Not that it is significantly shorter. But it also works. And the doc says explicitly that it does exactly (when no sep, no format is passed) what my 1st solution does.