我想操作 h5py 数据集的旧项目之一,然后删除旧项目并添加新项目。
我使用 __delitem__()
函数删除旧的数据集项。似乎成功地从 f5py 文件的 keys
中删除了该项目。但是文件大小加倍。任何人都可以建议实际删除 h5py 数据集的项目吗?非常感谢。
这是我的代码:
import numpy as np
import h5py
# suppose I have hdf5 file names stored in: h5_files
for name in h5_files:
roll_images = []
with h5py.File(name, "a") as f:
x = f["x_data"]
np_x = np.array(x)
# do something to np_x, but keep dtype and shape the same as x.
f.__delitem__("x_data")
f.create_dataset("x_data", data = np_x)
原始h5py文件的大小是:997.3MB。但是运行上面的代码后,文件大小大约翻倍:2.0GB
最佳答案
我可能是错的,但我认为删除数据集实际上只是删除了数据集的名称,但数据仍保留在文件中。这将解释文件大小加倍的原因。
如果您确实需要“删除”数据集,请将除数据集以外的所有数据复制到新的 hdf5 文件中。我记得这是我能够找到的唯一解决方法来实现同样的目标。
注意:您可以使用 del f["x_data"]
而不是 f.__delitem__("x_data")
。
关于python-2.7 - 删除 h5py 数据集的项目,但文件大小加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39448961/