检查h5py中是否存在节点

2024-04-20 11:54:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有一种简单的方法可以使用h5py检查HDF5文件中是否存在节点。

我在文档中找不到任何东西,所以现在我正在使用异常,这很难看。

# check if node exists
# first assume it exists
e = True
try:
  h5File["/some/path"]
except KeyError:
  e = False # now we know it doesn't

添加上下文:在尝试创建同名的新节点之前,我将使用此选项来确定节点是否存在。


Tags: 文件方法文档nodetrueif节点check
4条回答
e = "/some/path" in h5File

做到了。这在the ^{} documentation中非常简短地提到。

您还可以简单地对组使用require_group()方法。H5py Docs.

group docs检查文档之后。我假设您可以在使用前使用group对象的keys方法进行检查:

# check if node exists
# first assume it doesn't exist
e = False
node = "/some/path"
if node in h5file.keys():
    h5File[node]
    e = True

相关问题 更多 >