H5py store list of list of strings

前端 未结 2 1548
一整个雨季
一整个雨季 2021-01-18 09:16

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 segmentati

相关标签:
2条回答
  • 2021-01-18 10:06

    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)
    
    0 讨论(0)
  • 2021-01-18 10:08

    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'))
    
    0 讨论(0)
提交回复
热议问题