convert binary string to numpy array

Each Answer to this Q is separated by one/two green lines.

Assume I have the string:

my_data="\x00\x00\x80?\x00\x00\[email protected]\x00\[email protected]@\x00\x00\[email protected]"

Where I got it is irrelevant, but for the sake of having something concrete, assume I read it from a binary file.

I know my string is the binary representation of 4 (4-byte) floats. I would like to get those floats as a numpy array. I could do:

import struct
import numpy as np
tple = struct.unpack( '4f', my_data )
my_array = np.array( tple, dtype=np.float32 )

But it seems silly to create an intermediate tuple. Is there a way to do this operation without creating an intermediate tuple?


I would also like to be able to construct the array in such a way that I can specify the endianness of the string.

>>> np.frombuffer(b'\x00\x00\x80?\x00\x00\[email protected]\x00\[email protected]@\x00\x00\[email protected]', dtype="<f4") # or dtype=np.dtype('<f4'), or np.float32 on a little-endian system (which most computers are these days)
array([ 1.,  2.,  3.,  4.], dtype=float32)

Or, if you want big-endian:

>>> np.frombuffer(b'\x00\x00\x80?\x00\x00\[email protected]\x00\[email protected]@\x00\x00\[email protected]', dtype=">f4") # or dtype=np.dtype('>f4'), or np.float32  on a big-endian system
array([  4.60060299e-41,   8.96831017e-44,   2.30485571e-41,
         4.60074312e-41], dtype=float32)

The b isn’t necessary prior to Python 3, of course.

In fact, if you actually are using a binary file to load the data from, you could even skip the using-a-string step and load the data directly from the file with numpy.fromfile().

Also, dtype reference, just in case:

np.fromstring() is deprecated. Use np.frombuffer() instead.

import numpy as np

my_data = b'\x00\x00\x80?\x00\x00\[email protected]\x00\[email protected]@\x00\x00\[email protected]'

# np.fromstring is deprecated
# data = np.fromstring(my_data, np.float32)
data = np.frombuffer(my_data, np.float32)

[1. 2. 3. 4.]

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .