pythondictionaryhdf5h5py

How to store and load a Python dictionary with HDF5


I'm having issues loading (I think storing is working – a file is being created and contains data) a dictionary (string key and array/list value) from a HDF5 file. I'm receiving the following error:

ValueError: malformed node or string: < HDF5 dataset "dataset_1": shape (), type "|O" >

My code is:

import h5py

def store_table(self, filename):
    table = dict()
    table['test'] = list(np.zeros(7,dtype=int))

    with h5py.File(filename, "w") as file:
        file.create_dataset('dataset_1', data=str(table))
        file.close()


def load_table(self, filename):
    file = h5py.File(filename, "r")
    data = file.get('dataset_1')
    print(ast.literal_eval(data))

I've read online using the ast method literal_eval should work but it doesn't appear to help... How do I 'unpack' the HDF5 so it's a dictionary again?

Any ideas would be appreciated.


Solution

  • If I understand what you are trying to do, this should work:

    import numpy as np
    import ast
    import h5py
    
    
    def store_table(filename):
        table = dict()
        table['test'] = list(np.zeros(7,dtype=int))
    
        with h5py.File(filename, "w") as file:
            file.create_dataset('dataset_1', data=str(table))
    
    
    def load_table(filename):
        file = h5py.File(filename, "r")
        data = file.get('dataset_1')[...].tolist()
        file.close();
        return ast.literal_eval(data)
    
    filename = "file.h5"
    store_table(filename)
    data = load_table(filename)
    print(data)