python 中是否有一个函数可以显示 .hdf5 文件的完整结构?

nic*_*llo 5 python hdf5

打开.hdf5文件时,可以通过不同的方式探索文件的级别、键和名称。我想知道是否有一种方法或功能可以显示.hdf5. 最终显示出整棵树。

Ale*_*x44 7

对于所有想要继续使用h5py包的人:

\n

从实现的角度来看,这不是一句简单的话,但它可以与h5py包一起使用。通过这个递归函数,您可以将其用作单行函数:

\n
import h5py\n\nfilename_hdf = \'data.hdf5\'\n\ndef h5_tree(val, pre=\'\'):\n    items = len(val)\n    for key, val in val.items():\n        items -= 1\n        if items == 0:\n            # the last item\n            if type(val) == h5py._hl.group.Group:\n                print(pre + \'\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 \' + key)\n                h5_tree(val, pre+\'    \')\n            else:\n                print(pre + \'\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 \' + key + \' (%d)\' % len(val))\n        else:\n            if type(val) == h5py._hl.group.Group:\n                print(pre + \'\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 \' + key)\n                h5_tree(val, pre+\'\xe2\x94\x82   \')\n            else:\n                print(pre + \'\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 \' + key + \' (%d)\' % len(val))\n\nwith h5py.File(filename_hdf, \'r\') as hf:\n    print(hf)\n    h5_tree(hf)\n
Run Code Online (Sandbox Code Playgroud)\n


Azy*_*282 4

尝试使用nexuformatpackage 列出hdf5文件的结构。

\n\n

安装方式pip install nexusformat

\n\n

代码

\n\n
import nexusformat.nexus as nx\nf = nx.nxload(\xe2\x80\x98myhdf5file.hdf5\xe2\x80\x99)\nprint(f.tree)\n
Run Code Online (Sandbox Code Playgroud)\n\n

这应该打印文件的整个结构。有关更多信息,请参阅线程。可以找到例子 示例可以在这里

\n