python读取hdf5文件_Python 存储与读取HDF5文件

#!/usr/bin/python#-*- coding: UTF-8 -*-#

#Created by WW on Jan. 26, 2020#All rights reserved.#

importh5pyimportnumpy as npdefmain():#===========================================================================

#Create a HDF5 file.

f = h5py.File("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 ind.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 ing.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()if __name__ == "__main__":

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值