我有一段代码经历了几次迭代。在每次迭代中,代码都会生成一个基于numpy的数组。我将基于numpy的数组添加到现有的二进制.dat文件中。我使用下面的代码来生成数据:以数组的形式读取二进制.dat文件
WholeData = numpy.concatenate((Location,Data),axis=0)
# Location & Data are two numpy arrays
DataBinary = open('DataBinary.dat','ab')
WholeData.tofile(DataBinary)
DataBinary.close()
我想读取整个二进制文件到数组中。我有以下困难:
我尝试下面的代码:
NewData = numpy.array('f') File1 = open('DataBinary.dat','rb') NewData.fromstring(File1.read()) File1.close()
错误状态:
Traceback (most recent call last): File "", line 1, in AttributeError: 'numpy.ndarray' object has no attribute 'fromstring'
我试图使用基于阵列的阵列,然后阅读文件放入数组中。
from array import array File1 = open('DataBinary.dat','rb') NewData.fromstring(File1.read()) File1.close()
然而,NewData
是错误的,即,它不一样WholeData
。我想保存数据为numpy.array
并将其读取为array.array
可能不是一个好的选择。
任何建议将不胜感激。
这工作得很好!我会研究更好的方法来存储数据。非常感谢,mgilson。 – Nazmul 2012-08-03 15:48:34
这工作正常,以浮点形式读取数据。但是,如果我想要读取数据表,我应该怎么做?我想保留原始的数据框维度。 – hmi 2016-12-30 03:57:24