【问题标题】:How to read float values from binary data in Python?如何从 Python 中的二进制数据中读取浮点值?
【发布时间】:2021-03-16 09:39:38
【问题描述】:

我有一个二进制文件,其中散布着数据段。我知道每个数据段的位置(字节偏移),以及这些数据段的大小,以及数据点的类型(float、float32 - 意味着每个数据点都由 4 个字节编码)。我想将这些数据段读入一个类似结构的数组(例如,numpy 数组或 pandas 数据帧),但这样做有困难。我试过使用 numpy 的 memmap,但它在最后一个数据段上短路了,而 numpy 从文件中得到的结果很奇怪。

代码示例:

begin=datadf["$BEGINDATA"][0]     #datadf is pandas.df that has where data begins and its size
buf.seek(begin)                   #buf is the file that is opened in rb mode
size=datadf["$DATASIZE"][0]+1     #same as the above
data=buf.read(size)               #this should get me that data segment, but in binary

有没有办法从这个二进制数据可靠地转换为 float32。 为了进一步说明,我将前 10 个数据点的打印输出包括在内。

buf.seek(begin)
print(buf.read(40))    #10 points of float32 (4bytes) means 40
>>>b'\xa5\x10[@\x00\x00\x88@a\xf3\xf7A\x00\x00\x88@&\x93\x9bA\x00\x00\x88@\x00\x00\x00@\xfc\xcd\x08?\x1c\xe2\xbe?\x03\xf9\xa4?'

如果是任意值,虽然每个浮点数有 4 个字节(32 位宽),但每个浮点数的最大值为 10 000

【问题讨论】:

    标签: python python-3.x numpy binaryfiles


    【解决方案1】:

    如果你想要numpy.ndarray,你可以使用numpy.frombuffer

    >>> import numpy as np
    >>> data = b'\xa5\x10[@\x00\x00\x88@a\xf3\xf7A\x00\x00\x88@&\x93\x9bA\x00\x00\x88@\x00\x00\x00@\xfc\xcd\x08?\x1c\xe2\xbe?\x03\xf9\xa4?'
    >>> np.frombuffer(data, dtype=np.float32)
    array([ 3.422891 ,  4.25     , 30.993837 ,  4.25     , 19.44685  ,
            4.25     ,  2.       ,  0.5343931,  1.4912753,  1.2888492],
          dtype=float32)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 2017-06-28
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多