26

I have an existing hdf5 file with three arrays, i want to extract one of the arrays using h5py.

2 Answers 2

57

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).

Sign up to request clarification or add additional context in comments.

3 Comments

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
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).
For posterity: the .value method no longer works. Use f['array_name'][()] instead.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.