如何列出 h5py 文件中的所有数据集?

新手上路,请多包涵

我有一个存储 numpy 数组的 h5py 文件,但是当我尝试使用我记得的数据集名称打开它时,我得到了 Object doesn't exist error ,那么有没有一种方法可以列出该文件具有哪些数据集?

    with h5py.File('result.h5','r') as hf:
        #How can I list all dataset I have saved in hf?

原文由 matchifang 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 985
2 个回答

其他答案只是告诉您如何在根组下列出键,这可能会引用其他组或数据集。

如果你想要更接近 h5dump 但在 python 中的东西,你可以这样做:

 import h5py

def descend_obj(obj,sep='\t'):
    """
    Iterate through groups in a HDF5 file and prints the groups and datasets names and datasets attributes
    """
    if type(obj) in [h5py._hl.group.Group,h5py._hl.files.File]:
        for key in obj.keys():
            print sep,'-',key,':',obj[key]
            descend_obj(obj[key],sep=sep+'\t')
    elif type(obj)==h5py._hl.dataset.Dataset:
        for key in obj.attrs.keys():
            print sep+'\t','-',key,':',obj.attrs[key]

def h5dump(path,group='/'):
    """
    print HDF5 file metadata

    group: you can give a specific group, defaults to the root group
    """
    with h5py.File(path,'r') as f:
         descend_obj(f[group])

原文由 Seb 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题