如何使用h5py覆盖h5文件中的数组 [英] How to overwrite array inside h5 file using h5py
问题描述
我试图覆盖一个 numpy 数组,它是一个非常复杂的 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()
我尝试的代码看起来像这样但没有成功:
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()
作为完整性检查,我使用以下代码在 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?
推荐答案
您想赋值,而不是创建数据集:
You want to assign values, not create a dataset:
f1 = h5py.File(file_name, 'r+') # open the file
data = f1['meas/frame1/data'] # load the data
data[...] = X1 # assign new values to data
f1.close() # close the file
要确认更改已正确进行并保存:
To confirm the changes were properly made and saved:
f1 = h5py.File(file_name, 'r')
np.allclose(f1['meas/frame1/data'].value, X1)
#True
这篇关于如何使用h5py覆盖h5文件中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!