【问题标题】:H5py store list of list of stringsH5py 存储字符串列表列表
【发布时间】:2016-10-18 19:18:14
【问题描述】:

在 h5py 中是否有可能创建一个由字符串列表组成的数据集。我试图创建一个可变长度的嵌套数据类型,但这会导致我的 python 解释器出现分段错误。

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)

【问题讨论】:

    标签: python hdf5 h5py


    【解决方案1】:

    如果您不打算编辑 hdf5 文件(并可能使用更长的字符串),您也可以简单地使用:

    h5py_file.create_dataset("sentences", data=np.array(data, dtype='S'))
    

    【讨论】:

    【解决方案2】:

    如果您按照post 中的建议将数据定义为 dtype=object 的 numpy 数组,而不是列表列表,您应该能够获得所需的功能。

    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)
    

    【讨论】:

    • TypeError: Object dtype dtype('O') has no native HDF5 equivalent - 您的参考与 HDF 文件无关
    猜你喜欢
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多