Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to export HDF5 file to NumPy using H5PY?

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

like image 541
l.z.lz Avatar asked Apr 23 '12 03:04

l.z.lz


People also ask

How do I open an HDF5 file in Python?

To use HDF5, numpy needs to be imported. One important feature is that it can attach metaset to every data in the file thus provides powerful searching and accessing. Let's get started with installing HDF5 to the computer. As HDF5 works on numpy, we would need numpy installed in our machine too.

How do I create a h5py file?

Creating HDF5 filesThe first step to creating a HDF5 file is to initialise it. It uses a very similar syntax to initialising a typical text file in numpy. The first argument provides the filename and location, the second the mode. We're writing the file, so we provide a w for write access.


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

like image 173
Danica Avatar answered Sep 22 '22 23:09

Danica