在Python中读取HDF文件的属性
我在用pandas读取hdf文件时遇到了问题。目前,我不知道这个文件里的键是什么。
在这种情况下,我该怎么读取[data.hdf]这个文件呢?而且,我的文件是.hdf格式,不是.h5,这在获取数据时有区别吗?
我看到你需要一个“存储中的组标识符”。
pandas.io.pytables.read_hdf(path_or_buf, key, **kwargs)
我已经从pytables获取了元数据。
File(filename=data.hdf, title='', mode='a', root_uep='/', filters=Filters(complevel=0, shuffle=False, fletcher32=False, least_significant_digit=None))
/ (RootGroup) ''
/UID (EArray(317,)) ''
atom := StringAtom(itemsize=36, shape=(), dflt='')
maindim := 0
flavor := 'numpy'
byteorder := 'irrelevant'
chunkshape := (100,)
/X Y (EArray(8319, 2, 317)) ''
atom := Float32Atom(shape=(), dflt=0.0)
maindim := 0
flavor := 'numpy'
byteorder := 'little'
chunkshape := (1000, 2, 100)
那我该怎么让它在pandas中可读呢?
4 个回答
0
你可以使用这个简单的函数来查看任何HDF文件中的变量名称(这个方法只适用于科学模式下的变量)。
from pyhdf.SD import *
def HDFvars(File):
"""
Extract variable names for an hdf file
"""
# hdfFile = SD.SD(File, mode=1)
hdfFile = SD(File, mode=1)
dsets = hdfFile.datasets()
k = []
for key in dsets.keys():
k.append(key)
k.sort()
hdfFile.end() # close the file
return k
如果变量不在科学模式下,你可以尝试使用pyhdf.V,通过下面的程序来显示任何HDF文件中包含的vgroups的内容。
from pyhdf.HDF import *
from pyhdf.V import *
from pyhdf.VS import *
from pyhdf.SD import *
def describevg(refnum):
# Describe the vgroup with the given refnum.
# Open vgroup in read mode.
vg = v.attach(refnum)
print "----------------"
print "name:", vg._name, "class:",vg._class, "tag,ref:",
print vg._tag, vg._refnum
# Show the number of members of each main object type.
print "members: ", vg._nmembers,
print "datasets:", vg.nrefs(HC.DFTAG_NDG),
print "vdatas: ", vg.nrefs(HC.DFTAG_VH),
print "vgroups: ", vg.nrefs(HC.DFTAG_VG)
# Read the contents of the vgroup.
members = vg.tagrefs()
# Display info about each member.
index = -1
for tag, ref in members:
index += 1
print "member index", index
# Vdata tag
if tag == HC.DFTAG_VH:
vd = vs.attach(ref)
nrecs, intmode, fields, size, name = vd.inquire()
print " vdata:",name, "tag,ref:",tag, ref
print " fields:",fields
print " nrecs:",nrecs
vd.detach()
# SDS tag
elif tag == HC.DFTAG_NDG:
sds = sd.select(sd.reftoindex(ref))
name, rank, dims, type, nattrs = sds.info()
print " dataset:",name, "tag,ref:", tag, ref
print " dims:",dims
print " type:",type
sds.endaccess()
# VS tag
elif tag == HC.DFTAG_VG:
vg0 = v.attach(ref)
print " vgroup:", vg0._name, "tag,ref:", tag, ref
vg0.detach()
# Unhandled tag
else:
print "unhandled tag,ref",tag,ref
# Close vgroup
vg.detach()
# Open HDF file in readonly mode.
filename = 'yourfile.hdf'
hdf = HDF(filename)
# Initialize the SD, V and VS interfaces on the file.
sd = SD(filename)
vs = hdf.vstart()
v = hdf.vgstart()
# Scan all vgroups in the file.
ref = -1
while 1:
try:
ref = v.getid(ref)
print ref
except HDF4Error,msg: # no more vgroup
break
describevg(ref)
0
文档在这里。不过,你不能直接用pandas读取你展示的格式。你需要用PyTables来读取它。pandas可以直接读取PyTables的表格格式,即使没有pandas通常使用的元数据。
1
首先,文件后缀是.hdf还是.h5其实没有什么区别。
其次,我对pandas不太确定,但我读取HDF5文件的方式是这样的:
import h5py
h5f = h5py.File("test.h5", "r")
h5f.keys()
或者
h5f.values()
1
pyhdf
是在 Python 中处理 hdf 文件的一个替代选择。
你可以通过以下方式读取和查看键:
import pyhdf
hdf = pyhdf.SD.SD('file.hdf')
hdf.datasets()
希望这对你有帮助!祝好运!