如何使用h5py覆盖H5文件中数组 [英] How to overwrite array inside h5 file using h5py
问题描述
我试图覆盖numpy的阵列,这是一个pretty的一小部分复杂H5文件。
I'm trying to overwrite a numpy array that's a small part of a pretty complicated h5 file.
我解压一个数组,改变一些值,则需要重新插入阵列到H5文件。
I'm extracting an array, changing some values, then want to re-insert the array into the h5 file.
我没有问题,那提取的嵌套数组。
I have no problem extracting the array that's nested.
f1 = h5py.File(file_name,'r')
X1 = f1['meas/frame1/data'].value
f1.close()
我试图code看起来像这样没有成功:
My attempted code looks something like this with no success:
f1 = h5py.File(file_name,'r+')
dset = f1.create_dataset('meas/frame1/data', data=X1)
f1.close()
作为一个全面的检查,我使用下面的code执行该Matlab中,并没有任何问题的工作。
As a sanity check, I executed this in Matlab using the following code, and it worked with no problems.
h5write(file1, '/meas/frame1/data', X1);
有没有人对如何做到这一点有什么建议成功?
Does anyone have any suggestions on how to do this successfully?
推荐答案
askewchan的回答描述了做到这一点(您不能创建在已经存在的名称的数据集,但你当然可以修改该数据集的数据)。但是请注意,该数据集必须具有相同的形状作为数据( X1
)你正在写它。如果你想的替换的不同形状的其他一些数据集数据集,必须先删除它:
askewchan's answer describes the way to do it (you cannot create a dataset under a name that already exists, but you can of course modify the dataset's data). Note, however, that the dataset must have the same shape as the data (X1
) you are writing to it. If you want to replace the dataset with some other dataset of different shape, you first have to delete it:
del f1['meas/frame1/data']
dset = f1.create_dataset('meas/frame1/data', data=X1)
这篇关于如何使用h5py覆盖H5文件中数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!