I have an existing hdf5 file with three arrays, i want to extract one of the arrays using h5py.
2 Answers
h5py already reads files in as numpy arrays, so just:
with h5py.File('the_filename', 'r') as f:
my_array = f['array_name'][()]
The [()] means to read the entire array in; if you don't do that, it doesn't read the whole data but instead gives you lazy access to sub-parts (very useful when the array is huge but you only need a small part of it).
3 Comments
l.z.lz
Thanks a lot Dougal.I have modified the code to: >>> f = h5py.File('D:/!JODI/Macau Wind/u_100m/20100101.hdf5', 'r') my_array = f['u'].value f.close() Another stupid question is the out putarray is in a file? where can i find the output array? thank you so much
Danica
I'm not sure what you mean by the output array:
my_array from above? Any changes you make to that are only stored in memory unless you save them yourself (into an h5py.File or with something like numpy.save).Danica
For posterity: the
.value method no longer works. Use f['array_name'][()] instead.For this question it is way overkill but if you have a lot of things like this to do I use a package SpacePy that makes some of this easier.
datamodel.fromHDF5() documentation This returns a dictionary of arrays stored in a similar way to how h5py handles data.