读取.h5文件时,常用的是h5py模块,但该模块存在一些缺点,就是在python中,使用h5py读取的数据会对原始数据产生类似于转置等操作,在使用过程中带来诸多不便。 而hdf5storage模块可以完全避免这种问题。
hdf5storage的使用非常简单,示例如下:
import hdf5storage as hdf5
import numpy as np
import h5py
data1 = np.random.randn(4,4)
hdf5.write(data = data1, path = '/b', filename = './data1.h5',
store_python_metadata=True, matlab_compatible=True)
data_read = h5py.File('./data1.h5', 'r')
print(list(data_read.keys()))
参数:store_python_metadata, matlab_compatible保证了数据的可还原度,即数据在读取时的数据和存储时的状态一模一样。path:为数据的keys。
同时hdf5storage模块还支持mat file v’7.3’。只需将后缀.h5改为.mat即可。本文章建议只用hdf5storage模块存储,不用来读取。因为该模块难以获取h5文件的keys(),在不知道h5文件的结构下无法读取文件内容。读取h5文件还是要用h5py模块。