Skip to main content
 首页 » 编程设计

python之使用 Python 的 HDF 文件中的数据丢失

2025年05月04日27cmt

我正在尝试读取一个 hdf 文件,但没有显示组。我尝试了几种使用表和 h5py 的不同方法,但都无法在文件中显示组。我检查了一下,文件是“分层数据格式(版本 5)数据”(参见更新)。文件信息为here供引用。

可以找到示例数据here

import h5py 
import tables as tb 
 
hdffile = "TRMM_LIS_SC.04.1_2010.260.73132" 

使用 h5py:

f = h5py.File(hdffile,'w') 
print(f) 

输出:

< HDF5 file "TRMM_LIS_SC.04.1_2010.260.73132" (mode r+) > 
[] 

使用表格:

fi=tb.openFile(hdffile,'r') 
print(fi) 

输出:

TRMM_LIS_SC.04.1_2010.260.73132 (File) '' 
Last modif.: 'Wed Aug 10 18:41:44 2016' 
Object Tree: 
/ (RootGroup) '' 
 
Closing remaining open files:TRMM_LIS_SC.04.1_2010.260.73132...done 

更新

h5py.File(hdffile,'w') overwrote the file and emptied it. 

现在我的问题是如何将 hdf 版本 4 文件读入 python,因为 h5py 和表都不起作用?

请您参考如下方法:

文件有多大?我认为 h5py.File(hdffile,'w') 会覆盖它,所以它是空的。使用 h5py.File(hdffile,'r') 读取。

我没有足够的业力来回复@Luke H 的回答,但将它读入 pandas 可能不是一个好主意。 Pandas hdf5 使用 pytables,这是一种使用 hdf5 的“自以为是”的方式。这意味着它存储额外的元数据(例如索引)。所以我只会使用 pytables 来读取文件,如果它是用 pytables 制作的。