如何列出 h5py 文件中的所有数据集?
How to list all datasets in h5py file?
我有一个存储 numpy 数组的 h5py 文件,但在尝试使用我记得的数据集名称打开它时出现 Object doesn't exist error
,那么有没有办法列出该文件包含的数据集?
with h5py.File('result.h5','r') as hf:
#How can I list all dataset I have saved in hf?
你必须使用按键方法。这将为您提供数据集和组名称的 unicode 字符串列表。
例如:
Datasetnames=hf.keys()
另一种基于图形用户界面的方法是使用 HDFView。
https://support.hdfgroup.org/products/java/release/download.html
如果你想列出键名,你需要使用keys()方法给你一个键对象,然后使用list()方法列出键:
with h5py.File('result.h5','r') as hf:
dataset_names = list(hf.keys())
只是为了显示底层数据集的名称,我会简单地使用 h5dump -n <filename>
没有 运行 python 脚本。
其他答案只是告诉你如何在根组下做一个键列表,可能会引用其他组或数据集。
如果你想要更接近 h5dump 但在 python 中的东西,你可以这样做:
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])
如果您使用的是命令行,请按照其他人的建议使用 h5ls -r [file]
或 h5dump -n [file]
。
在 python 内,如果您想在最顶层的组下面列出但不想编写自己的代码来降低树,请尝试 visit() 函数:
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)
因为使用 keys()
函数只会为您提供顶级键,并且还将包含组名和数据集(正如 Seb), you should use the visit()
function (as suggested by jasondet 已经指出的那样)并且只保留指向的键到数据集。
这个答案是 jasondet's and Seb 对一个简单函数的答案的合并:
def get_dataset_keys(f):
keys = []
f.visit(lambda key : keys.append(key) if isinstance(f[key], h5py.Dataset) else None)
return keys
我有一个存储 numpy 数组的 h5py 文件,但在尝试使用我记得的数据集名称打开它时出现 Object doesn't exist error
,那么有没有办法列出该文件包含的数据集?
with h5py.File('result.h5','r') as hf:
#How can I list all dataset I have saved in hf?
你必须使用按键方法。这将为您提供数据集和组名称的 unicode 字符串列表。 例如:
Datasetnames=hf.keys()
另一种基于图形用户界面的方法是使用 HDFView。 https://support.hdfgroup.org/products/java/release/download.html
如果你想列出键名,你需要使用keys()方法给你一个键对象,然后使用list()方法列出键:
with h5py.File('result.h5','r') as hf:
dataset_names = list(hf.keys())
只是为了显示底层数据集的名称,我会简单地使用 h5dump -n <filename>
没有 运行 python 脚本。
其他答案只是告诉你如何在根组下做一个键列表,可能会引用其他组或数据集。
如果你想要更接近 h5dump 但在 python 中的东西,你可以这样做:
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])
如果您使用的是命令行,请按照其他人的建议使用 h5ls -r [file]
或 h5dump -n [file]
。
在 python 内,如果您想在最顶层的组下面列出但不想编写自己的代码来降低树,请尝试 visit() 函数:
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)
因为使用 keys()
函数只会为您提供顶级键,并且还将包含组名和数据集(正如 Seb), you should use the visit()
function (as suggested by jasondet 已经指出的那样)并且只保留指向的键到数据集。
这个答案是 jasondet's and Seb 对一个简单函数的答案的合并:
def get_dataset_keys(f):
keys = []
f.visit(lambda key : keys.append(key) if isinstance(f[key], h5py.Dataset) else None)
return keys