-1

I would like to ask if there any method to store the output of h5py's visit in a string? The code below is intended to print all the entries in a hdf5 file with the h5py's method visit(), but I find myself with the need to store each of the printed entries in memory rather than printing them out on the screen.

def printname(name):
    print(name)
    
with h5py.File(filename, 'r') as f:
    f.visit(printname)
fsaizpoy
  • 149
  • 1
  • 2
  • 8

1 Answers1

1

All you need to do is create a global variable to hold the data. See example below. It creates a file with some groups and datasets, then calls visititems(). It creates and returns a dictionary of names and object types. I prefer visititems() over visit() so I can reference the h5 node as an object. You should be able to use this as a template for your process.

def visit_all_nodes(name, node):
    global node_dict
    node_dict = {} 
    fullname = node.name
    if isinstance(node, h5py.Dataset):
    # node is a dataset
        print(f'adding Dataset: {fullname}')
        node_dict[fullname] = 'Dataset'
    elif isinstance(node, h5py.Group):
     # node is a group
        print(f'adding Group: {fullname}')
        node_dict[fullname] = 'Group'
    else:
     # node type not dataset or group
        print(f'Node: {fullname}; skipping')
        
with h5py.File('File.h5','w') as h5f:  
    h5f.create_group('Group')
    h5f.create_group('Group/subGroup')
    h5f.create_dataset('Group/subGroup/NotaName',shape=(10,10))
    h5f.create_group('Group2')
    h5f.create_group('Group2/subGroup')
    h5f.create_dataset('Group2/subGroup/Name',shape=(10,10))       

    print ('**Walking file to get all nodes**\n')
    h5f.visititems(visit_all_nodes)
    print()
    for k,v in node_dict.items():
        print(f'Node name: {k}, Type: {v}')
kcw78
  • 7,131
  • 3
  • 12
  • 44