How to list a dataset in an h5py file?

I have a h5py file that stores numpy arrays, but I got Object doesn't exist errorwhen I try to open it with the name of the data set that I remember, so that I can specify which data files the file has?

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

You must use the key method. This will give you a list of Unicode strings of your dataset and group names. For instance:

Datasetnames=hf.keys()

Another GUI based method is to use HDFView. https://support.hdfgroup.org/products/java/release/download.html

+5
source

, keys(), , list() :

with h5py.File('result.h5','r') as hf:
    dataset_names = list(hf.keys())
+6

, , .

- 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])
+2

h5dump -n <filename>

python script.

+1

, , visit():

with h5py.File('result.h5','r') as hf:
    hf.visit(print)

Or for something more advanced (e.g. to include attribute information) use visititems:

def printall(name, obj):
    print(name, dict(obj.attrs))

with h5py.File('result.h5','r') as hf:
    hf.visititems(printall)
+1
source

Source: https://habr.com/ru/post/1680623/


All Articles