Python: h5py快速上手

Python: h5py快速上手


1. 数据保存到h5 / hdf5文件

import h5py
import numpy as np
a = np.arange(5).reshape((1,1,1,5))
with h5py.File('test.h5', 'w') as f:
    f['data'] = a

2. 从h5 / hdf5文件读取数据

import h5py
import numpy as np
data = h5py.File('test.h5', 'r')
a = data['data'][:]

查看key:

> print(f.keys())
[u'data']

若打印结果如KeysView(),则可以遍历输出:

> for k in data.keys():
>   print(k)
data

更多参考

  • h5py Quick Start Guide on GitHub
    https://github.com/h5py/h5py/blob/master/docs/quick.rst

你可能感兴趣的:(Python)