Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何遍历hdf5文件中的所有键和值,并确定哪些包含数据?_Python_Hdf5_H5py - Fatal编程技术网

Python 如何遍历hdf5文件中的所有键和值,并确定哪些包含数据?

Python 如何遍历hdf5文件中的所有键和值,并确定哪些包含数据?,python,hdf5,h5py,Python,Hdf5,H5py,我将模型模拟的结果存储在hdf5文件(.hdf)中 我知道如何使用h5py模块打开文件并仔细阅读数据 问题是,嵌套的键和数据集太多了,要真正找到所有这些键和数据集并确定其中哪些键和数据集包含数据是一件非常痛苦的事情 这就是我目前正在处理的问题: import h5py f = h5py.File('results.hdf') #to read the file k1 = f.keys() #shows the keys in the first level k1 <KeysViewHD

我将模型模拟的结果存储在hdf5文件(.hdf)中

我知道如何使用h5py模块打开文件并仔细阅读数据

问题是,嵌套的键和数据集太多了,要真正找到所有这些键和数据集并确定其中哪些键和数据集包含数据是一件非常痛苦的事情

这就是我目前正在处理的问题:

import h5py
f = h5py.File('results.hdf') #to read the file

k1 = f.keys() #shows the keys in the first level

k1
<KeysViewHDF5 ['Event Conditions', 'Geometry', 'Plan Data', 'Results']>
导入h5py
f=h5py.File('results.hdf')#读取文件
k1=f.keys()#显示第一级中的键
k1
现在,要查看存储的所有数据,我可以执行以下操作:

for k1 in f:
    for k2 in f[k1].keys():
        for k3 in f[k1][k2].keys():
            print(f[k1][k2][k3])  

<HDF5 group "/Event Conditions/Unsteady/Boundary Conditions" (2 members)>
<HDF5 group "/Event Conditions/Unsteady/Initial Conditions" (0 members)>
<HDF5 dataset "Attributes": shape (350,), type "|V45">
<HDF5 dataset "Polyline Info": shape (350, 4), type "<i4">
<HDF5 dataset "Polyline Parts": shape (350, 2), type "<i4">
<HDF5 dataset "Polyline Points": shape (3598, 2), type "<f8">
<HDF5 dataset "Attributes": shape (3,), type "|V37">
<HDF5 dataset "Polygon Info": shape (3, 4), type "<i4">
<HDF5 dataset "Polygon Parts": shape (3, 2), type "<i4">
<HDF5 dataset "Polygon Points": shape (344, 2), type "<f8">
<HDF5 dataset "Attributes": shape (1,), type "|V64">
<HDF5 dataset "Cell Info": shape (1, 2), type "<i4">
<HDF5 dataset "Cell Points": shape (586635, 2), type "<f8">
<HDF5 group "/Geometry/2D Flow Areas/Delta" (0 members)>
<HDF5 group "/Geometry/2D Flow Areas/Perimeter 1" (25 members)>
<HDF5 dataset "Polygon Info": shape (1, 4), type "<i4">
<HDF5 dataset "Polygon Parts": shape (1, 2), type "<i4">
<HDF5 dataset "Polygon Points": shape (610, 2), type "<f8">
<HDF5 dataset "Attributes": shape (1,), type "|V60">
<HDF5 dataset "External Faces": shape (177,), type "|V24">
<HDF5 dataset "Polyline Info": shape (1, 4), type "<i4">
<HDF5 dataset "Polyline Parts": shape (1, 2), type "<i4">
<HDF5 dataset "Polyline Points": shape (5, 2), type "<f8">
<HDF5 dataset "TIN Info": shape (347, 4), type "<i4">
<HDF5 dataset "TIN Points": shape (13591, 4), type "<f8">
<HDF5 dataset "TIN Triangles": shape (20008, 3), type "<i4">
<HDF5 dataset "XSIDs": shape (347, 2), type "<i4">
<HDF5 dataset "Attributes": shape (348,), type "|V676">
<HDF5 group "/Geometry/Cross Sections/Flow Distribution" (5 members)>
<HDF5 dataset "Manning's n Info": shape (348, 2), type "<i4">
<HDF5 dataset "Manning's n Values": shape (1044, 2), type "<f4">
<HDF5 dataset "Polyline Info": shape (348, 4), type "<i4">
<HDF5 dataset "Polyline Parts": shape (348, 2), type "<i4">
<HDF5 dataset "Polyline Points": shape (696, 2), type "<f8">
<HDF5 dataset "Station Elevation Info": shape (348, 2), type "<i4">
<HDF5 dataset "Station Elevation Values": shape (151973, 2), type "<f4">
<HDF5 dataset "Attributes": shape (41,), type "|V32">
<HDF5 dataset "Calibration Table": shape (2,), type "|V200">
<HDF5 dataset "Polygon Info": shape (41, 4), type "<i4">
<HDF5 dataset "Polygon Parts": shape (41, 2), type "<i4">
<HDF5 dataset "Polygon Points": shape (45442, 2), type "<f8">
<HDF5 dataset "Polyline Info": shape (2, 4), type "<i4">
<HDF5 dataset "Polyline Parts": shape (2, 2), type "<i4">
<HDF5 dataset "Polyline Points": shape (1768, 2), type "<f8">
<HDF5 dataset "Attributes": shape (1,), type "|V96">
<HDF5 dataset "Polyline Info": shape (1, 4), type "<i4">
<HDF5 dataset "Polyline Parts": shape (1, 2), type "<i4">
<HDF5 dataset "Polyline Points": shape (2042, 2), type "<f8">
<HDF5 dataset "Polyline Info": shape (2, 4), type "<i4">
<HDF5 dataset "Polyline Parts": shape (2, 2), type "<i4">
<HDF5 dataset "Polyline Points": shape (1152, 2), type "<f8">
<HDF5 dataset "Attributes": shape (1,), type "|V253">
<HDF5 dataset "Centerline Info": shape (1, 4), type "<i4">
<HDF5 dataset "Centerline Parts": shape (1, 2), type "<i4">
<HDF5 dataset "Centerline Points": shape (48, 2), type "<f8">
<HDF5 dataset "Profiles": shape (500,), type "|V28">
<HDF5 dataset "Compute Messages (rtf)": shape (1,), type "|S293107">
<HDF5 dataset "Compute Messages (text)": shape (1,), type "|S215682">
<HDF5 dataset "Compute Processes": shape (6,), type "|V332">
<HDF5 group "/Results/Unsteady/Geometry Info" (3 members)>
<HDF5 group "/Results/Unsteady/Output" (1 members)>
<HDF5 group "/Results/Unsteady/Summary" (0 members)>
f中k1的
:
对于f[k1]中的k2,键()
对于f[k1][k2]中的k3。键():
打印(f[k1][k2][k3])
但如果我继续这样做,首先它开始变得荒谬,显然有一个更干净的方式,其次它开始崩溃,因为一些关键点只下降了一定数量的水平

我想知道hdf文件中数据的所有可能键/路径,以及它们是否包含数据(有些不包含)

可能是某种带有try/except的循环,用于处理路径的结尾

如果你知道怎么做,请帮助任何人

谢谢。

来自,文档链接如下:

def打印属性(名称、对象):
印刷品(名称)
对于key,obj.attrs.items()中的val:
打印(“%s:%s”%(键,val))
f=h5py.File('foo.hdf5','r')
f、 访问(打印属性)

它使用委托模式。您需要传递一个可调用的,并且
h5py
将使用名称和对象值调用它。在您的callable中,您可以检查并决定要做什么。

-检查
visit
方法。您的迭代必须足够智能,以避免这些崩溃;这是基本的Python编程。您可以测试下一级是否有
,或者将该步骤包装在
try/except
子句中。您可以从操作系统外壳调用类似
h5dump
的函数。它有很多选项。我得到:AttributeError:“AttributeManager”对象没有属性“iteritems”。。。知道我为什么/如何解决这个问题吗?我可以使用以print name结尾的函数列出所有路径。看起来h5py API已经改变了。我有一个使用
visititems
的例子,所以回答:()谢谢..这是可行的,但在我的文件运行到一半时崩溃了:TypeError:不存在TypeBitfieldID的NumPy等价物可能只是路径中的数据有点奇怪??不确定..但在任何情况下,我都可以使用你的例子和f.visit方法来获得所有名称的列表…并将其缩减为仅包含数据的数据集