您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

将二进制字符串转换为numpy数组

将二进制字符串转换为numpy数组

>>> 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

其他 2022/1/1 18:26:20 有555人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶