如何在h5py文件中列出数据集?

时间:2017-07-03 10:22:46

标签: h5py

我有一个存储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?

6 个答案:

答案 0 :(得分:6)

如果要列出键名,则需要使用keys()方法为您提供键对象,然后使用list()方法列出键:

with h5py.File('result.h5','r') as hf:
    dataset_names = list(hf.keys())

答案 1 :(得分:2)

您必须使用keys方法。这将为您提供数据集和组名称的unicode字符串列表。 例如:

Datasetnames=hf.keys()

另一种基于gui的方法是使用HDFView。 https://support.hdfgroup.org/products/java/release/download.html

答案 2 :(得分:1)

只是为了显示基础数据集的名称,我只想使用# global variables are prefixed with funcname__ to avoid conflicts # look up color codes for our terminal rather than assuming ANSI set_prompt__red=$(tput setaf 1) set_prompt__green=$(tput setaf 2) set_prompt__reset=$(tput sgr0) set_prompt() { # only rerun this code when changing directories! if [[ $set_prompt__lastPWDl != "$PWD" ]]; then set_prompt__lastPWDl=$PWD set_prompt__lastPWDp=$(pwd -P) if [[ "$set_prompt__lastPWDl" = "$set_prompt__lastPWDp" ]]; then set_prompt__pwd_color="$set_prompt__red" else set_prompt__pwd_color="$set_prompt__green" fi fi # ...actually could have just "return"ed above, but this way we can change other # aspects of the prompt even when we don't need to do a new directory lookup. PS1='...whatever prefix...' PS1+="\[${set_prompt__pwd_color}\]${PWD}\[${set_prompt__reset}\]" PS1+='...whatever suffix...' } PROMPT_COMMAND=set_prompt

没有运行python脚本。

答案 3 :(得分:1)

其他答案仅告诉您如何列出根组下的键的列表,该根组可能引用其他组或数据集。

如果您想要更接近h5dump但在python中的内容,则可以执行以下操作:

{{1}}

答案 4 :(得分:1)

如果要在最上面的组下面列出,但又不想编写自己的代码来使树下降,请尝试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)

答案 5 :(得分:1)

由于使用 keys() 函数只会为您提供顶级键并且还将包含组名称和数据集(正如 Seb 已经指出的那样),您应该使用 {{1 }} 函数(如 jasondet 建议的那样)并仅保留指向数据集的键。

这个答案是 jasondetSeb 对一个简单函数的回答的合并:

visit()
相关问题