问题
How do I see what variables, datasets, etc. a given .h5 file has in Python?
I can read the file by running this
import h5py
f = h5py.File(filename, 'r')
How can I now see which variables my .h5 file have?
Running f.keys()
outputs the non-informative
KeysView(<HDF5 file filename (mode r)>)
In Matlab I simply call h5disp(filename) but would like to know how to do it in Python
回答1:
Did you try?
print(list(f.keys()))
That should give you all the group inside your hdf5 file. You can do the same for the datasets if f is a group.
回答2:
Maybe overkill, but I had this and might be useful for someone:
from __future__ import print_function
def scan_hdf5(path, recursive=True, tab_step=2):
def scan_node(g, tabs=0):
print(' ' * tabs, g.name)
for k, v in g.items():
if isinstance(v, h5.Dataset):
print(' ' * tabs + ' ' * tab_step + ' -', v.name)
elif isinstance(v, h5.Group) and recursive:
scan_node(v, tabs=tabs + tab_step)
with h5.File(path, 'r') as f:
scan_node(f)
And simple input:
>>> scan_hdf5('/tmp/dummy.h5')
/
- /d1
/g1
- /g1/d2
- /g1/d3
/g2
- /g2/d4
/g2/g3
- /g2/g3/d5
Or an alternative version that returns the elements in something more usable:
def scan_hdf52(path, recursive=True, tab_step=2):
def scan_node(g, tabs=0):
elems = []
for k, v in g.items():
if isinstance(v, h5.Dataset):
elems.append(v.name)
elif isinstance(v, h5.Group) and recursive:
elems.append((v.name, scan_node(v, tabs=tabs + tab_step)))
return elems
with h5.File(path, 'r') as f:
return scan_node(f)
with returns:
>>> scan_hdf5_2('/tmp/dummy.h5')
[u'/d1',
(u'/g1', [u'/g1/d2', u'/g1/d3']),
(u'/g2', [u'/g2/d4', (u'/g2/g3', [u'/g2/g3/d5'])])]
来源:https://stackoverflow.com/questions/43371438/how-to-inspect-h5-file-in-python