How to read HDF5 files that only have datasets (no groups) using h5py?

I have HDF5 files that I would like to open using the Python h5py module (in Python 2.7).

This is easy when I have a file with groups and data sets:

import h5py as hdf

with hdf.File(relative_path_to_file, 'r') as f:
    my_data = f['a_group']['a_dataset'].value

However, in my current situation, I have no groups. There are only data sets. Unfortunately, I can’t access my data no matter what I try. None of the following works (all break with KeyErrors or ValueErrors):

my_data = f['a_dataset'].value #KeyError

my_data = f['/a_dataset'].value #KeyError

my_data = f['/']['a_dataset'].value #KeyError

my_data = f['']['a_dataset'].value #ValueError

my_data = f['.']['a_dataset'].value #KeyError

I can redo my files to have a group if there is no solution. There seems to be a solution, though ...

H5py doesn't seem to see any keys:

f.keys()
[]
+4
source share
3

, , , h5py.

( , ) , hdf5 relative. , .

, , ...

gspr jimmyb : -)

+3

, :

f['.']['a_dataset']

:

dir(f['/']) 
dir(f['.'])
+1

, .

:

fileName = "data/hdf5/topo.hdf5"

with h5py.File(fileName, 'w') as f:
    dset = f.create_dataset('topography', data = z, dtype = 'float32')

:

with h5py.File(fileName, 'r') as f:
    my_data = f['.']['topography'].value
0

All Articles