原因
h5py 的String类型只接受ASCII的数据,如果你是Python3或者Python使用utf-8的编码就会报错。
import h5py
f = h5py.File('test.hdf', 'w')
f.attrs.create('test', '1234134')
>>> h5py\h5t.pyx in h5py.h5t.py_create()
>>> TypeError: No conversion path for dtype: dtype('<U7')
解决方法
需要使用h5py的special_dtype函数,str对应着Python3的字符串类型。
import h5py
dtype = h5py.special_dtype(vlen=str)
f = h5py.File('test.hdf', 'w')
f.attrs.create('test', '1234134', dtype=dtype)
http://docs.h5py.org/en/latest/strings.html 官方文档关于String类型的介绍
本文介绍如何解决在使用h5py库时遇到的无法存储UTF-8编码字符串的问题,通过使用special_dtype函数指定字符串类型,实现Python3环境下对h5py文件的正确写入。
1649

被折叠的 条评论
为什么被折叠?



