How to Read and Write HDF5 Data file in Python

Including Cython, Jython, IronPython, PyPy, Django framework, and interpreters: Ipython, IPython Jupyter/Notebook, CPython


Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5307
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

HDF stands for Hierarchical Data Format, a well known file format for storing and organizing large amounts of numerical data . In python HDF5 data files can be handled by means of the h5py module. This article explains how to read and write hdf5 files in Python. See also h5py quick start, How to access HDF5 data from Python, and Read HDF 5 file in Python

See a quick example in post #2 below.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5307
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#2

Here is a quick example on how to create, save and read data into and from hdf5 files in Python (See more examples here).

  1. import numpy as np
  2. import h5py
  3.  
  4. #Create data files, here we create two random matrices
  5. data_file1 = np.random.random(size = (1000, 1000))
  6. data_file2 = np.random.random(size = (500, 400))
  7.  
  8. #Save data files in the hdf5 format in the file hdf5_data
  9. with h5py.File('/home/tssfl/Desktop/hdf5_data.h5', 'w') as hdf:
  10.     hdf.create_dataset("data_set1", data = data_file1)
  11.     hdf.create_dataset("data_set2", data = data_file2)
  12.  
  13. #Read the dataset:
  14. f = h5py.File('/home/tssfl/Desktop/hdf5_data.h5', 'r')
  15.  
  16. #Check the dataset keys
  17. f.keys()
  18. Out[3]: [u'data_set1', u'data_set2']
  19.  
  20. #Get specific data files
  21. data1 = f['data_set1']
  22.  
  23. #Check data shape
  24. data1.shape
  25. Out[5]: (1000, 1000)
  26.  
  27. #Similarly for data_set2
  28. data2 = f['data_set2']
  29. data2.shape
  30. Out[7]: (500, 400)

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Python Programming”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests