【问题标题】:How to write lots of ndarray into a file, where each ndarray is saved in a single line?如何将大量 ndarray 写入文件,其中每个 ndarray 保存在一行中?
【发布时间】:2020-07-31 02:26:45
【问题描述】:

我有很多 ndarray 对象要保存。我想一个一个地保存这些 ndarray,这意味着每个 ndarray 都保存在文件中的一行中。我发现 np.savez 似乎对这种情况没有用。我怎样才能做到这一点?谢谢!

我试过这样的方式:

当保存这些 ndarray 时,

with open(file, 'a') as f:
  for i in range(n)
    f.write(str(ndarry[i].tostring()) + '\n')

当加载和恢复它们时,

list_array = []
with open(file, 'a') as f:
  line = f.reanline().strip('\n')
  while line
    ndarray = np.fromstring(line, dtype=np.int64).reshape((2,3))
    list_array.append(ndarray)
    line = f.reanline().strip('\n')

但我得到“ValueError:字符串大小必须是元素大小的倍数”

【问题讨论】:

  • 请展示您尝试过的内容。您是否希望将nnp.arrays 保存到同一个文件中?
  • 另外所有的数组长度都一样吗?
  • 我试过在保存 ndarray 时使用 f.write(str(np.tostring()) + '\n') 和 np.fromstring(f.readline().strip('\n ')) 来恢复它,但我得到“ValueError:字符串大小必须是元素大小的倍数”。我确实想将 n np.arrays 保存到同一个文件中。
  • 编辑问题以包含您尝试过的内容
  • 是的,所有的数组都具有相同的形状。

标签: python numpy numpy-ndarray


【解决方案1】:

您是否尝试仅针对一个阵列调试行写入/读取?详细看步骤?

In [568]: arr = np.arange(3)                                                                           
In [569]: arr                                                                                          
Out[569]: array([0, 1, 2])
In [570]: arr.tostring()                                                                               
Out[570]: b'\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00'

b 告诉我们这是一个字节串(就像旧的 py2 字符串)。然后你将它“包装”在 py3 字符串中:

In [571]: str(arr.tostring())+'\n'                                                                     
Out[571]: "b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x00\\x00\\x00\\x00'\n"

现在尝试阅读:

In [572]: _.strip('\n')                                                                                
Out[572]: "b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x00\\x00\\x00\\x00'"
In [573]: np.fromstring(_, np.int64)                                                                   
/usr/local/bin/ipython3:1: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
  #!/usr/bin/python3
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-573-fa8feb7879b7> in <module>
----> 1 np.fromstring(_, np.int64)

ValueError: string size must be a multiple of element size

我可以从tostring 输出中恢复原始数组:

In [574]: np.frombuffer(arr.tostring(), np.int64)                                                      
Out[574]: array([0, 1, 2])

所有用于将二进制字符串写入文件的 str 和换行符都会破坏读取。

【讨论】:

  • 我也试过这样,只是我用np.frombuffer(f.readline().strip('\n'), dtype=np.int64) 来恢复每个ndarray。但是,我也得到了相同的“ValueError:字符串大小必须是元素大小的倍数”。顺便说一句,我用的是python2。
猜你喜欢
  • 2018-04-12
  • 2015-11-08
  • 2018-10-28
  • 2017-07-13
  • 2018-07-15
  • 2015-07-31
  • 1970-01-01
  • 2019-02-12
  • 2021-06-01
相关资源
最近更新 更多