In python the h5py module is used to read the primary key method in h5 file

  • 2020-11-03 22:30:59
  • OfStack

As shown below:


import h5py
import numpy as np
#HDF5 Write: 
imgData = np.zeros((2,4))
f = h5py.File('HDF5_FILE.h5','w') # create 1 a h5 File, the file pointer is f
f['data'] = imgData     # The primary key to write the data to the file data The following 
f['labels'] = np.array([1,2,3,4,5])   # The primary key to write the data to the file labels The following 
f.close()       # Close the file 
#HDF5 Read: 
f = h5py.File('HDF5_FILE.h5','r') # Open the h5 file 
#  You can view all the primary keys 
for key in f.keys():
 print(f[key].name)
 print(f[key].shape)
 print(f[key].value)

Output results:


E:\phthon35\python.exe I:/catsVSdogs1-master/catsVSdogs1-master/file01/test10.py
/data
(2, 4)
[[ 0. 0. 0. 0.]
 [ 0. 0. 0. 0.]]
/labels
(5,)
[1 2 3 4 5]
Process finished with exit code 0

Related articles: