【问题标题】:save binary binary columns with numpy用 numpy 保存二进制二进制列
【发布时间】:2015-08-02 11:17:39
【问题描述】:

我读了一些二进制列

dtype = ['<f8', '<f8', '<i4']
raw = np.fromfile(file_id, dtype=dtype, count=n_col)
f0 = raw[0]
f1 = raw[1]
i0 = raw[2]

现在我只有numpy 数组f0f1i0。我如何以同样的方式将它们写入二进制文件。试过了

np.array([f0, f1, i0]).tofile(file_id) # Promotes my int32 to double
np.array(tuple([f0, f1, i0]).tofile(file_id) # Does something writing a lot of data, but I have no idea in what structure

任何帮助都将不胜感激。

【问题讨论】:

    标签: file python-3.x numpy


    【解决方案1】:

    我发现的一个可能的解决方案是首先将所有内容类型转换为一个巨大的 int32 块,然后将其写下来。由于我没有找到任何类型转换,所以我复制了整个内存。我正在做这样的事情:

    raw = []
    # Kind of make a very ugly typecast by using a lot of Memomory
    tmp = np.fromstring(f0.tostring(), dtype=np.int32)
    raw.append(tmp[::2])
    raw.append(tmp[1::2])
    
    ... # do the same of f1
    
    raw.append(i0)
    
    # Finaly write it down
    np.array(raw).transpose().tofile(file_id)
    

    由于这个解决方案是相当丑陋的记忆,所以我要到星期一才会接受它。也许有人可以在这里提供更好的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 2012-05-17
      • 2020-04-18
      • 2015-08-20
      • 2020-06-07
      • 1970-01-01
      相关资源
      最近更新 更多