I have a HDF5 file with multiple folders inside. Each folder has attributes added (some call attributes "metadata"). I know how to access the keys inside a folder, but I don't know how to pull the attributes with Python's h5py package. Here are attributes from HDFView:
Folder1(800,4)
Group size = 9
Number of attributes = 1
measRelTime_seconds = 201.73
I need to pull this measRelTime_seconds
value. I already have a loop to read files
f = h5py.File(file,'r')
for k,key in enumerate(f.keys()): #loop over folders
#need to obtain measRelTime_seconds here, I guess
Thanks
Attributes work just like groups and datasets. Use object.attrs.keys()
to iterate over the attribute names. The object could be a file, group or dataset.
Here is a simple example that creates 2 attributes on 3 different objects, then reads and prints them.
arr = np.random.randn(1000)
with h5py.File('groups.hdf5', 'w') as f:
g = f.create_group('Base_Group')
d = g.create_dataset('default', data=arr)
f.attrs['User'] = 'Me'
f.attrs['OS'] = 'Windows'
g.attrs['Date'] = 'today'
g.attrs['Time'] = 'now'
d.attrs['attr1'] = 1.0
d.attrs['attr2'] = 22.2
for k in f.attrs.keys():
print('{} => {}'.format(k, f.attrs[k]))
for k in g.attrs.keys():
print('{} => {}'.format(k, g.attrs[k]))
for k in d.attrs.keys():
print('{} => {}'.format(k, d.attrs[k]))
Ok, I find my answer. To read it You can simply check the name of the attribute as
f['Folder'].attrs.keys()
and the value can be returned with
f['Folder'].attrs['<name of the attribute>']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.