8

Is there a possibility in h5py to create a dataset which consists of lists of strings. I tried to create a nested datatype of variable length, but this results in segmentation fault in my python interpreter.

def create_dataset(h5py_file):
    data = [['I', 'am', 'a', 'sentecne'], ['another', 'sentence']]
    string_dt = h5py.special_dtype(vlen=str)
    nested_dt = h5py.special_dtype(vlen=string_dt)
    h5py_file.create_dataset("sentences", data=data, dtype = nested_dt)
Kakey
  • 3,860
  • 5
  • 28
  • 45
PKuhn
  • 1,288
  • 13
  • 29

2 Answers2

7

If you don't intend to edit the hdf5 file (and potentially use longer strings), you can also simply use:

h5py_file.create_dataset("sentences", data=np.array(data, dtype='S'))
jan-glx
  • 5,998
  • 1
  • 37
  • 54
  • This will also will lead to problems if your data contains non-ASCII characters, read more about storing strings in HDF here: http://docs.h5py.org/en/stable/strings.html – jan-glx Jun 09 '20 at 13:15
2

You should be able to get the functionality you want if you define your data as a numpy array of dtype=object as suggested in this post, rather than a list of lists.

def create_dataset(h5py_file):
    data = np.array([['I', 'am', 'a', 'sentence'], ['another', 'sentence']], dtype=object)
    string_dt = h5py.special_dtype(vlen=str)
    h5py_file.create_dataset("sentences", data=data, dtype=string_dt)
Community
  • 1
  • 1
Heather QC
  • 620
  • 8
  • 11
  • `TypeError: Object dtype dtype('O') has no native HDF5 equivalent` - your reference has nothing to do with HDF files – Ed S. May 05 '21 at 18:43