cs205-lecture-examples

Example codes used during Harvard CS205 lectures
git clone https://git.0xfab.ch/cs205-lecture-examples.git
Log | Files | Refs | README | LICENSE

hdf5_in_python.py (544B)


      1 #!/usr/bin/env python3
      2 # File       : hdf5_in_python.py
      3 # Description: Example to load a HDF5 data file in Python
      4 # Copyright 2023 Harvard University. All Rights Reserved.
      5 import h5py as h5
      6 
      7 
      8 def load_hdf5(fname, group):
      9     with h5.File(fname, 'r') as data:
     10         return data[group][()]
     11 
     12 
     13 if __name__ == '__main__':
     14     data = load_hdf5(
     15         'data.h5', group='u'
     16     )  # you could have many groups in a HDF5 file!
     17     print(data.shape)  # print dimension of data
     18     print(data[0, 0, :])  # prints the first 8 data entries in field `u`