我想创建一个包含"组"的名称的python列表。来自HDF5文件。我可以使用以下代码执行此操作" groups"是一个全局变量,我想让它成为一个局部变量,这样我就可以读取多个HDF5文件,并为该文件提供一个本地的组列表,而不是所有文件。
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import h5py
# this is global and not what I want - want this to be local
groups=[]
# use with visit items ob is info about group or dataset
def printname2(name,ob):
print "\t",name,' ==> ',ob
groups.append(name) # i want to replace this, how do I save "ob" in a list???
def print_attrs(f,s):
print '\n'+s,' ',f[s]
for i in range( len(f[s].attrs.keys()) ):
print '\t',f[s].attrs.keys()[i],'\t\t',f[s].attrs.values()[i]
filename2 = './testFile.hdf5'
f = h5py.File(filename2, 'r')
print 'f.visit traverses recursively through the file structure'
#groups[] --- I tried to re-define groups here but doesn't work...
f.visititems(printname2) # groups is updated ; fix this
print '\nNumber of groups ',len(groups),'\n'
#
attributes=[]
key=[]
for g in groups:
key.append ( f[g].attrs.keys() )
print '\\nDUMP ALL GROUPS AND ATTRIBUTES PARSED ", len(groups)
for g in groups:
print_attrs(f,g)