numpy's tostring/fromstring --- what do I need to specify to restore the array

numpy, portability, python

Solution

`sys.byteorder` gives the endianness of the machine.

However, as @J.F.Sebastain, @seberg and @jorgeca have suggested, `np.savez` is a better way to go. The help docstring shows

import io
content = io.BytesIO()
np.savez(content, x=x, y=y)
content.seek(0)

which means you could save the string `content` to an sqlite database.

Then, when you SELECT this string from the database, it can be re-converted into numpy arrays with

data = np.load(content)

Problem

Given a raw binary representation of a `numpy` array, what is the complete set of metadata needed to unambiguously restore the array? For example, ``` >>> np.fromstring( np.array([42]).tostring()) array([ 2.07507571e-322]) ``` which is to be expected (with a hindsight, at least): here the I haven't told `fromstring` to expect ints, so it goes with the default float. But it seems to me that just specifying the `dtype=np.float64` or similar may or may not be sufficient. For example, ``` >>> a = np.array([42.]) >>> a.dtype dtype('float64') >>> a.dtype.byteorder '=' ``` which the docs tell me means 'native order'. Meaning, it's going to be interpreted differently on a big-endian and little-endian machines --- or am I missing something simple?

Original source