python开源库——h5py快速入门指南
python开源库——h5py快速指南
HDF5 数据文件简介
HDF5 文件及 h5py
HDF5 简介
HDF(Hierarchical Data Format)指一种为存储和处理大容量科学数据设计的文件格式及相应库文件。HDF 最早由美国国家超级计算应用中心 NCSA 开发,目前在非盈利组织 HDF 小组维护下继续发展。当前流行的版本是 HDF5。HDF5 拥有一系列的优异特性,使其特别适合进行大量科学数据的存储和操作,如它支持非常多的数据类型,灵活,通用,跨平台,可扩展,高效的 I/O 性能,支持几乎无限量(高达 EB)的单文件存储等,详见其官方介绍:https://support.hdfgroup.org/HDF5/ 。
Python 中有一系列的工具可以操作和使用 HDF5 数据,其中最常用的是 h5py 和 PyTables。我们只介绍 h5py。
h5py
一个 HDF5 文件是存储两类对象的容器,这两类对象分别为:
- dataset:类似数组的数据集合;
- gropp;类似目录的容器,其中可以包含一个或多个 dataset 及其它的 group。
一个 HDF5 文件从一个命名为 "/" 的 group 开始,所有的 dataset 和其它 group 都包含在此 group 下,当操作 HDF5 文件时,如果没有显式指定 group 的 dataset 都是默认指 "/" 下的 dataset,另外类似相对文件路径的 group 名字都是相对于 "/" 的。
HDF5 文件的 dataset 和 group 都可以拥有描述性的元数据,称作 attribute。
用 h5py 操作 HDF5 文件,我们可以像使用目录一样使用 group,像使用 numpy 数组一样使用 dataset,像使用字典一样使用属性,非常方便和易用。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# Created by WW on Jan. 26, 2020
# All rights reserved.
#
import h5py
import numpy as np
def main():
#===========================================================================
# Create a HDF5 file.
f = h5py.File("D:/desktop/h5py_example.hdf5", "w") # mode = {'w', 'r', 'a'}
# Create two groups under root '/'.
g1 = f.create_group("bar1")
g2 = f.create_group("bar2")
# Create a dataset under root '/'.
d = f.create_dataset("dset", data=np.arange(16).reshape([4, 4]))
# Add two attributes to dataset 'dset'
d.attrs["myAttr1"] = [100, 200]
d.attrs["myAttr2"] = "Hello, world!"
# Create a group and a dataset under group "bar1".
c1 = g1.create_group("car1")
d1 = g1.create_dataset("dset1", data=np.arange(10))
# Create a group and a dataset under group "bar2".
c2 = g2.create_group("car2")
d2 = g2.create_dataset("dset2", data=np.arange(10))
# Save and exit the file.
f.close()
''' h5py_example.hdf5 file structure
+-- '/'
| +-- group "bar1"
| | +-- group "car1"
| | | +-- None
| | |
| | +-- dataset "dset1"
| |
| +-- group "bar2"
| | +-- group "car2"
| | | +-- None
| | |
| | +-- dataset "dset2"
| |
| +-- dataset "dset"
| | +-- attribute "myAttr1"
| | +-- attribute "myAttr2"
| |
|
'''
#===========================================================================
# Read HDF5 file.
f = h5py.File("h5py_example.hdf5", "r") # mode = {'w', 'r', 'a'}
# Print the keys of groups and datasets under '/'.
print(f.filename, ":")
print([key for key in f.keys()], "\n")
#===================================================
# Read dataset 'dset' under '/'.
d = f["dset"]
# Print the data of 'dset'.
print(d.name, ":")
print(d[:])
# Print the attributes of dataset 'dset'.
for key in d.attrs.keys():
print(key, ":", d.attrs[key])
print()
#===================================================
# Read group 'bar1'.
g = f["bar1"]
# Print the keys of groups and datasets under group 'bar1'.
print([key for key in g.keys()])
# Three methods to print the data of 'dset1'.
print(f["/bar1/dset1"][:]) # 1. absolute path
print(f["bar1"]["dset1"][:]) # 2. relative path: file[][]
print(g['dset1'][:]) # 3. relative path: group[]
# Delete a database.
# Notice: the mode should be 'a' when you read a file.
'''
del g["dset1"]
'''
# Save and exit the file
f.close()
快去成为你想要的样子!
浙公网安备 33010602011771号