h5py提供了一个模型datasets and groups。前者基本上是数组,后者你可以将其视为目录。每个都有名字。您应该查看 API 的文档和示例:
http://docs.h5py.org/en/latest/quick.html http://docs.h5py.org/en/latest/quick.html
一个简单的示例,您预先创建所有数据并且只想将其保存到 hdf5 文件中,如下所示:
In [1]: import numpy as np
In [2]: import h5py
In [3]: a = np.random.random(size=(100,20))
In [4]: h5f = h5py.File('data.h5', 'w')
In [5]: h5f.create_dataset('dataset_1', data=a)
Out[5]: <HDF5 dataset "dataset_1": shape (100, 20), type "<f8">
In [6]: h5f.close()
然后您可以使用以下命令加载该数据:
'
In [10]: h5f = h5py.File('data.h5','r')
In [11]: b = h5f['dataset_1'][:]
In [12]: h5f.close()
In [13]: np.allclose(a,b)
Out[13]: True
一定要查看文档:
http://docs.h5py.org http://docs.h5py.org
写入 hdf5 文件取决于 h5py 或 pytables(每个都有不同的 python API,位于 hdf5 文件规范之上)。您还应该看看 numpy 本身提供的其他简单二进制格式,例如np.save
, np.savez
etc:
http://docs.scipy.org/doc/numpy/reference/routines.io.html http://docs.scipy.org/doc/numpy/reference/routines.io.html