Skip to content Skip to sidebar Skip to footer

Attributeerror: 'int' Object Has No Attribute 'encode' Hdf5

I'm trying to open a HDF5 file in Python using the following code: with h5py.File('example.hdf5', 'r') as f: ls = list(f.keys()) dat = f.get('data') dt = np.array(dat)

Solution 1:

Here is a code snippet to test your keys for type (Group or Dataset). It uses the visititems() method to recursively walk nodes in your file and report each as a 1) Group, 2) Dataset, 3) Object dataset, or 4) Unknown. Once you find a dataset, you can read and create a NumPy array. However, that is not required. You can work with a h5py dataset object "as-if" it is a NumPy array.

def visitor_func(name, node):
    if isinstance(node, h5py.Group):
        print(node.name, 'is a Group')
    elif isinstance(node, h5py.Dataset):
       if (node.dtype == 'object') :
            print (node.name, 'is an object Dataset')
       else:
            print(node.name, 'is a Dataset')   
    else:
        print(node.name, 'is an unknown type')         

#####    

print ('checking hdf5 file')
with h5py.File('example.hdf5', 'r') as h5f:
    h5f.visititems(visitor_func) 

Post a Comment for "Attributeerror: 'int' Object Has No Attribute 'encode' Hdf5"