En ouvrant un fichier hdf5 avec le module hdf5 de python, on obtient des dictionnaires contenus dans des dictionnaires qu'on peut visualiser de la façon suivante en utilisant une récurrence:
import h5py
def print_hdf5_tree(
val, # initial dictionnary
pre='',
):
items = len(val)
for key, val in val.items():
items -= 1
if items == 0:
# the last item
if type(val) == h5py._hl.group.Group:
print(pre + '└── ' + key)
print_hdf5_tree(val, pre+' ')
else:
try:
print(pre + '└── ' + key + ' (%d)' % len(val))
except TypeError:
print(pre + '└── ' + key + ' (scalar)')
else:
if type(val) == h5py._hl.group.Group:
print(pre + '├── ' + key)
print_hdf5_tree(val, pre+'│ ')
else:
try:
print(pre + '├── ' + key + ' (%d)' % len(val))
except TypeError:
print(pre + '├── ' + key + ' (scalar)')
def show_hdf5_tree(
fname, # hdf5 file
):
"""
show the structure of a hdf5 file.
"""
with h5py.File(fname, 'r') as hf:
print(hf)
print_hdf5_tree(hf)