我有一个存储numpy数组的h5py文件,但在尝试使用我记得的数据集名称打开它时,出现了“对象不存在错误”,那么我能以某种方式列出文件中包含哪些数据集吗?
with h5py.File('result.h5','r') as hf:
#How can I list all dataset I have saved in hf?
Datasetnames=hf.keys()
另一种基于图形界面的方法是使用HDFView。 https://support.hdfgroup.org/products/java/release/download.html
import h5py
def descend_obj(obj,sep='\t'):
"""
Iterate through groups in a HDF5 file and prints the groups and datasets names and datasets attributes
"""
if type(obj) in [h5py._hl.group.Group,h5py._hl.files.File]:
for key in obj.keys():
print sep,'-',key,':',obj[key]
descend_obj(obj[key],sep=sep+'\t')
elif type(obj)==h5py._hl.dataset.Dataset:
for key in obj.attrs.keys():
print sep+'\t','-',key,':',obj.attrs[key]
def h5dump(path,group='/'):
"""
print HDF5 file metadata
group: you can give a specific group, defaults to the root group
"""
with h5py.File(path,'r') as f:
descend_obj(f[group])
with h5py.File('result.h5','r') as hf:
dataset_names = list(hf.keys())
f[key]
是继承自 h5py.Dataset
的类的实例,那么 isinstance(f[key], h5py.Dataset)
可能会更好,尽管这种情况可能很少发生。 - ajz34h5ls -r [文件]
或h5dump -n [文件]
。with h5py.File('result.h5','r') as hf:
hf.visit(print)
如果需要更高级的功能(例如包括属性信息),可以使用 visititems:
def printall(name, obj):
print(name, dict(obj.attrs))
with h5py.File('result.h5','r') as hf:
hf.visititems(printall)
h5dump -n <filename>
,而无需运行Python脚本。