2012-08-01 75 views
11

想知道是否有一種簡單的方法來檢查一個節點是否存在於使用h5py的HDF5文件中。檢查節點是否存在於h5py中

我在文檔中找不到任何東西,所以現在我使用異常,這很醜陋。

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

要添加背景:我用這個來決定嘗試創建具有相同名稱的新節點之前的節點存在。

回答

0

在檢查文檔group docs後。我想你可以使用組對象的按鍵方法使用前檢查:

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

在Python 2,這實際上將整個組鍵的加載到一個列表,然後做了這個名單線性搜索,而使用'__contains__'(即h5file中的''/ some/path')將更直接地檢查它。另外,只有當它是頂級成員時,它纔會適用於給出的示例。 – Dougal 2012-08-01 07:47:23

+0

我考慮過這個問題,但它不適用於嵌入式成員。另外,我不知道效率的影響......謝謝! – 2012-08-01 08:01:34

相關問題