>>> 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)
The b
当然,在 Python 3 之前不需要。
事实上,如果您实际上使用二进制文件来加载数据,您甚至可以跳过使用字符串步骤并直接从文件加载数据numpy.fromfile()
.
另外,dtype 参考,以防万一:http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html