心在远方

导航

Python h5py

1、关于安装:

如果你使用的是Anaconda的话,安装命令如下:

conda install h5py

如果没有,安装命令如下:

pip install h5py

2、核心概念

读取HDF5文件,假如现有一个HDF5文件test.hdf5

>>> import h5py
>>> f = h5py.File("test.hdf5", "r")

第一行导入h5py模块,第二行读取文件为File object。对File object操作就行Python的字典相似;

>>> list(f.keys())
['mydataset']

这样我们可以查看key,就像字典一样。经查看里面有一个数据“mydataset”,我们可以提取该数据;

>>> dset = f["mydataset"]

读取出来的数据更像是Python中的Numpy arrays对象;

>>> dset.shape
(100,)
>>> dset.dtype
dtype('int32')

支持数组的切片;

>>> dset[...] = np.arange(100)
>>> dset[0]
0
>>> dset[10]
10
>>> dset[1:100:10]
array([0, 10, 20, 30, 40, 50, 60, 70, 80, 90])

3、创建一个文件和数据

>>> import h5py
>>> import numpy as np
>>> f = h5py.File("mytestfile.hdf5", "w")
>>> dset = f.create_dataset("mydataset", (100,), dtype='i')

4、组和层级组织

>>> dset.name
u'/mydataset'

The "folders" in this system are called groups, The File object we create is itself a group, in this case the root group ,name /

>>> f.name
u'/'

当然也可以创建子组;

>>> dset2 = h5py.File('mydataset..hdf5', 'a')
>>> grp = f.create_group("subgroup")
>>> dset2 = grp.create_dataset('another_dataset", (50,), dtype='f')
>>> dset2.name
u'/subgroup/another_dataset'

可以直接指定路径创建;

>>> dset3 = f.create_dataset('subgroup2/dataset_three', (10,), dtype='i')
>>> dset3.name
u'/subgroup2/dataset_three'

 

posted on 2018-10-29 11:36  韩伟123  阅读(3650)  评论(0编辑  收藏  举报