【发布时间】:2016-03-23 16:00:51
【问题描述】:
我使用 Python 包 h5py(版本 2.5.0)来访问我的 hdf5 文件。
我想遍历文件的内容并对每个数据集做一些事情。
使用visit 方法:
import h5py
def print_it(name):
dset = f[name]
print(dset)
print(type(dset))
with h5py.File('test.hdf5', 'r') as f:
f.visit(print_it)
对于我获得的测试文件:
<HDF5 group "/x" (1 members)>
<class 'h5py._hl.group.Group'>
<HDF5 dataset "y": shape (100, 100, 100), type "<f8">
<class 'h5py._hl.dataset.Dataset'>
这告诉我文件中有一个数据集和一个组。然而,除了使用type() 来区分数据集和组之外,没有明显的方法。不幸的是,h5py documentation 没有提及这个话题。他们总是假设您事先知道什么是组和什么是数据集,例如因为他们自己创建了数据集。
我想要类似的东西:
f = h5py.File(..)
for key in f.keys():
x = f[key]
print(x.is_group(), x.is_dataset()) # does not exist
使用 h5py 在 Python 中读取未知 hdf5 文件时,如何区分组和数据集?如何获取所有数据集、所有组、所有链接的列表?
【问题讨论】: