将二进制字符串转换为numpy数组 [英] convert binary string to numpy array

查看:165
本文介绍了将二进制字符串转换为numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有字符串:

my_data = '\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@'

我在哪里得到它是无关紧要的,但是为了具体起见,假设我从二进制文件中读取了它.

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

我知道我的字符串是4个(4字节)浮点数的二进制表示形式.我想将那些浮点数作为一个numpy数组.我可以做:

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.fromstring(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)

或者,如果您想要大端的话:

Or, if you want big-endian:

>>> np.fromstring(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)

当然,在Python 3之前不需要b.

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

实际上,如果您实际上是使用二进制文件从中加载数据,甚至可以跳过使用字符串的步骤,并直接使用numpy.fromfile()从文件中加载数据.

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().

dtype参考,以防万一: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

Also, dtype reference, just in case: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

这篇关于将二进制字符串转换为numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆