>>> np.frombuffer(b'\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@', 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)
或者,如果您想要大端字节序:
>>> np.frombuffer(b'\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@', 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)
的b
到Python 3不是现有必要的,当然。
实际上,如果您实际上是使用二进制文件从中加载数据,则甚至可以跳过using-a- string步骤,并使用直接从文件中加载数据numpy.fromfile()
。
另外,以防万一,请输入dtype参考:http ://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html