26

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

Kermit
  • 32,563
  • 10
  • 80
  • 117
l.z.lz
  • 383
  • 1
  • 4
  • 13

2 Answers2

56

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

Danica
  • 26,635
  • 6
  • 86
  • 118
  • 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 – l.z.lz Apr 23 '12 at 03:55
  • 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 Apr 23 '12 at 07:13
  • 6
    For posterity: the `.value` method no longer works. Use `f['array_name'][()]` instead. – Danica Jan 04 '14 at 21:29
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.

Brian Larsen
  • 1,672
  • 16
  • 26