Search code examples
pythonhdf5h5py

write HDF h5 dataset (via h5py) which is a mix of string and numpy list


I have the following two datasets (I have several of these tuples):

filename_string: "something"
filename_list: [1,2,3,4,5] # this is a numpy array.

Id like to know how to write this in a compact format via h5py. The goal is to have the end user read this h5 datafile and be able to deduce the list and its corresponding filename.

I am able to efficiently write the numpy list to h5, but strings seems to be a big problem and errors out when I include this.

Any help would be great - wasted a few hours looking for a solution!


Solution

  • This little scrap of code will create a dataset named something (from the variable filename_string) that contains the data in your list filename_list.

    import h5py
    filename_string= "something"
    filename_list= [1,2,3,4,5]
    
    with h5py.File('SO_63137136.h5','w') as h5f:
        h5f.create_dataset(filename_string, data=filename_list)