python3_h5py_hdf5_遍历_查看文件结构
转载注明来源: 本文链接 来自osnosn的博客,写于 2020-03-26.
- pandas 的 hdf5 支持函数,好像没办法把 HDF5 文件的结构列出来。
- 只好使用
h5py
,把HDF5文件的结构打印出来。输出的是文本信息- python 3.6, 以下都是用
pip3 install
安装的 - h5py 2.10.0 (h5py的文档)
- numpy 1.18.2
- pandas 1.0.3 (hdf5 的支持, 需要安装 tables)
- tables 3.6.1 (PyTables' documentation)
- python 3.6, 以下都是用
方法1:
#!/usr/bin/python3
# -- coding:utf8 --
fname='myfilename.hdf5'
import h5py
def prt(name):
print(name)
f = h5py.File(fname,'r')
f.visit(prt)
f.close()
方法2:
#!/usr/bin/python3
# -- coding:utf8 --
fname='myfilename.hdf5'
import h5py
import numpy as np
def h5list(f,tab):
print(tab,'Group:',f.name,'len:%d'%len(f))
mysp2=tab[:-1]+ ' |-*'
for vv in f.attrs.keys(): # 打印属性
print(mysp2,end=' ')
print('%s = %s'% (vv,f.attrs[vv]))
mysp=tab[:-1] + ' |-'
for k in f.keys():
d = f[k]
if isinstance(d,h5py.Group):
h5list(d,mysp)
elif isinstance(d,h5py.Dataset):
print(mysp,'Dataset:',d.name,'(size:%d)'%d.size)
mysp1=mysp[:-1]+ ' |-'
print(mysp11,'(dtype=%s)'%d.dtype)
if d.dtype.names is not None:
print(mysp,end=' ')
for vv in d.dtype.names:
print(vv,end=',')
print()
mysp2=mysp1[:-1]+ ' |-*'
for vv in d.attrs.keys(): # 打印属性
print(mysp2,end=' ')
try:
print('%s = %s'% (vv,d.attrs[vv]))
except TypeError as e:
print('%s = %s'% (vv,e))
except:
print('%s = ?? Other ERR'% (vv,))
#print(d[:12]) # 打印12组数据看看
else:
print('??->',d,'Unkown Object!')
f = h5py.File(fname,'r')
h5list(f,'')
f.close()
- 写入属性(attrs)到hdf5文件,请参看python3_pandas.HDFStore_h5py_HDF5_的笔记