【问题标题】:Read or construct an array from a binary file containing both integers and doubles in Python/NumPy在 Python/NumPy 中从包含整数和双精度的二进制文件中读取或构造数组
【发布时间】:2016-04-22 18:06:39
【问题描述】:

我有一个包含整数和双精度的二进制文件。我想通过一次调用(例如:x = np.fromfile(f, dtype=np.int))或按顺序(逐个值)访问该数据。但是,NumPy 似乎不允许在不指定类型的情况下从二进制文件中读取。我应该将所有内容都转换为双倍,还是忘记 NumPy?

编辑。假设文件的格式是这样的:

int

int int int double double double double double double double

等等

【问题讨论】:

  • 查看struct 模块。
  • 为什么不能逐个读取值,假设你知道文件的格式,从而知道哪个是整数,哪个是双精度,然后用提升的类型填充 numpy 数组(例如所有浮动)?
  • “我有一个包含整数和双精度的二进制文件” - 这个二进制文件的格式是什么? “二进制文件”不足以说明这些数据是如何表示的;没有更多信息,我们无法判断如何读取此文件。
  • 这个例子可能会有所帮助。 stackoverflow.com/questions/14215715/…
  • 如果您需要有关特定示例的帮助,请考虑发布文件的简短示例以及您想要的输出内容:) 另请参阅 minimal reproducible example

标签: python arrays numpy binaryfiles


【解决方案1】:
NumPy doesn't seem to allow to read from a binary file without specifying a type

我所知道的任何编程语言都没有假装能够猜测原始二进制数据的类型;并且有充分的理由。您要解决的更高层次的问题到底是什么?

【讨论】:

    【解决方案2】:

    我认为您不需要 numpy。基本的 Python 二进制库 struct 正在完成这项工作。如果需要,将最后给出的元组列表转换为 numpy 数组。

    有关来源,请参阅 https://docs.python.org/2/library/struct.html 和 @martineau Reading a binary file into a struct in Python

    from struct import pack,unpack
    
    with open("foo.bin","wb") as file:
        a=pack("<iiifffffff", 1,2,3, 1.1,2.2e-2,3.3e-3,4.4e-4,5.5e-5,6.6e-6,7.7e-7 )
        file.write(a)
    
    with open("foo.bin","r") as file:
        a=unpack("<iiifffffff",file.read() )
        print a
    

    输出:

    (1, 2, 3, 1.100000023841858, 0.02199999988079071, 0.0032999999821186066, 0.0004400000034365803, 5.500000042957254e-05, 6.599999778700294e-06, 7.699999855503847e-07)
    

    在二进制编辑器(Frhed)中显示二进制文件:

    #how to read same structure repeatedly
    import struct
    
    fn="foo2.bin"
    struct_fmt = '<iiifffffff' 
    struct_len = struct.calcsize(struct_fmt)
    struct_unpack = struct.Struct(struct_fmt).unpack_from
    
    with open(fn,"wb") as file:
        a=struct.pack("<iiifffffff", 1,2,3, 1.1,2.2e-2,3.3e-3,4.4e-4,5.5e-5,6.6e-6,7.7e-7 )
        for i in range(3): 
            file.write(a)
    
    
    results = []
    with open(fn, "rb") as f:
        while True:
            data = f.read(struct_len)
            if not data: break
            s = struct_unpack(data)
            results.append(s)
    
    print results
    

    输出:

     [(1, 2, 3, 1.100000023841858, 0.02199999988079071, 0.0032999999821186066, 0.0004400000034365803, 5.500000042957254e-05, 6.599999778700294e-06, 7.699999855503847e-07), (1, 2, 3, 1.100000023841858, 0.02199999988079071, 0.0032999999821186066, 0.0004400000034365803, 5.500000042957254e-05, 6.599999778700294e-06, 7.699999855503847e-07), (1, 2, 3, 1.100000023841858, 0.02199999988079071, 0.0032999999821186066, 0.0004400000034365803, 5.500000042957254e-05, 6.599999778700294e-06, 7.699999855503847e-07)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 2014-01-26
      • 2012-12-02
      • 2010-11-12
      • 2011-10-13
      • 1970-01-01
      相关资源
      最近更新 更多