【发布时间】:2019-05-19 04:09:33
【问题描述】:
我正在尝试使用 python 中的h5py 包调整数据集的大小并存储新值。我的数据集大小在每次实例中都在不断增加,我想使用resize 函数附加.h5 文件。但是,我使用我的方法遇到了错误。变量dset 是一个数据集数组。
import os
import h5py
import numpy as np
path = './out.h5'
os.remove(path)
def create_h5py(path):
with h5py.File(path, "a") as hf:
grp = hf.create_group('left')
dset = []
dset.append(grp.create_dataset('voltage', (10**4,3), maxshape=(None,3), dtype='f', chunks=(10**4,3)))
dset.append(grp.create_dataset('current', (10**4,3), maxshape=(None,3), dtype='f', chunks=(10**4,3)))
return dset
if __name__ == '__main__':
dset = create_h5py(path)
for i in range(3):
if i == 0:
dset[0][:] = np.random.random(dset[0].shape)
dset[1][:] = np.random.random(dset[1].shape)
else:
dset[0].resize(dset[0].shape[0]+10**4, axis=0)
dset[0][-10**4:] = np.random.random((10**4,3))
dset[1].resize(dset[1].shape[0]+10**4, axis=0)
dset[1][-10**4:] = np.random.random((10**4,3))
编辑
感谢tel 我能够解决这个问题。将with h5py.File(path, "a") as hf: 替换为hf = h5py.File(path, "a")。
【问题讨论】:
-
主要问题是
dset显示为[<Closed HDF5 dataset>, <Closed HDF5 dataset>]