Python 使用 h5py 删除 hdf5 数据集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31861724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:40:49  来源:igfitidea点击:

Deleting hdf5 dataset using h5py

pythondatasethdf5h5py

提问by hsnee

Is there any way to remove a dataset from an hdf5 file, preferably using h5py? Or alternatively, is it possible to overwrite a dataset while keeping the other datasets intact?

有没有办法从 hdf5 文件中删除数据集,最好使用 h5py?或者,是否可以覆盖一个数据集同时保持其他数据集完好无损?

To my understanding, h5py can read/write hdf5 files in 5 modes

据我了解,h5py 可以在 5 种模式下读/写 hdf5 文件

f = h5py.File("filename.hdf5",'mode')

where mode can be rfor read, r+for read-write, afor read-write but creates a new file if it doesn't exist, wfor write/overwrite, and w-which is same as wbut fails if file already exists. I have tried all but none seem to work.

其中,模式可以是r读,r+读,写,a读,写,但如果创建它不存在,一个新的文件,w用于写/改写, w-这是一样的w,但如果文件已经存在失败。我已经尝试了所有但似乎没有工作。

Any suggestions are much appreciated.

任何建议都非常感谢。

采纳答案by EnemyBagJones

Yes, this can be done.

是的,这是可以做到的。

with h5py.File(input,  "a") as f:
    del f[datasetname]

You will need to have the file open in a writeable mode, for example append (as above) or write.

您需要以可写模式打开文件,例如追加(如上)或写入。

As noted by @seppo-enarvi in the comments the purpose of the previously recommendedf.__delitem__(datasetname)function is to implement thedeloperator, so that one can delete a dataset usingdel f[datasetname]

正如@seppo-enarvi 在评论中所指出的,先前推荐的f.__delitem__(datasetname)函数的目的是实现del运算符,以便可以使用del f[datasetname]

回答by agomcas

I do not understand what has your question to do with the file open modes. For read/write r+ is the way to go.

我不明白您的问题与文件打开模式有什么关系。对于读/写 r+ 是要走的路。

To my knowledge, removing is not easy/possible, in particular no matter what you do the file size will not shrink.

据我所知,删除并不容易/不可能,特别是无论你做什么,文件大小都不会缩小。

But overwriting content is no problem

但是覆盖内容没问题

f['mydataset'][:] = 0

回答by Felix

I tried this out and the only way I could actually reduce the size of the file is by copying everything to a new file and just leaving out the dataset I was not interested in:

我试过了,我实际上可以减小文件大小的唯一方法是将所有内容复制到一个新文件中,而只留下我不感兴趣的数据集:

fs = h5py.File('WFA.h5', 'r')
fd = h5py.File('WFA_red.h5', 'w')
for a in fs.attrs:
    fd.attrs[a] = fs.attrs[a]
for d in fs:
    if not 'SFS_TRANSITION' in d: fs.copy(d, fd)