从Python将字符串列表存储到HDF5数据集 [英] Storing a list of strings to a HDF5 Dataset from Python

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

问题描述

我正在尝试将可变长度的字符串列表存储到HDF5数据集.的代码是

I am trying to store a variable length list of string to a HDF5 Dataset. The code for this is

import h5py
h5File=h5py.File('xxx.h5','w')
strList=['asas','asas','asas']  
h5File.create_dataset('xxx',(len(strList),1),'S10',strList)
h5File.flush() 
h5File.Close()  

我收到一条错误消息,指出"TypeError:dtype没有转换路径:dtype('& lt U3')" 其中& lt表示实际小于符号
我怎么解决这个问题.

I am getting an error stating that "TypeError: No conversion path for dtype: dtype('&lt U3')" where the &lt means actual less than symbol
How can I solve this problem.

推荐答案

您正在读取Unicode字符串,但将数据类型指定为ASCII.根据 h5py Wiki ,h5py当前不存在支持这种转换.

You're reading in Unicode strings, but specifying your datatype as ASCII. According to the h5py wiki, h5py does not currently support this conversion.

您需要将字符串编码为h5py句柄格式:

You'll need to encode the strings in a format h5py handles:

asciiList = [n.encode("ascii", "ignore") for n in strList]
h5File.create_dataset('xxx', (len(asciiList),1),'S10', asciiList)

注意:并非所有以UTF-8编码的内容都可以以ASCII编码!

Note: not everything encoded in UTF-8 can be encoded in ASCII!

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

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