H5py存储字符串列表列表 [英] H5py store list of list of strings

查看:316
本文介绍了H5py存储字符串列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

推荐答案

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

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'))

这篇关于H5py存储字符串列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆