如何在HDF5文件中保存Python dtype('object')?

时间:2013-04-17 16:03:08

标签: python numpy hdf5

我正在尝试将.mat文件中的数据加载到python中,以便我可以将其保存在HDF5文件中。

问题是scipy.io.loadmat返回包含dtype = [('计数',' | O4')的数组的字典])

每当我尝试创建一个返回其中一个数组的数据集时,我都会收到以下错误:

>> mat = scipy.io.loadmat('state-10.mat')
>> h = h5py.File('test.hdf5','w')
>> h.create_dataset('set', data=mat['ProteinComplex'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/h5py/_hl/group.py", line 69, in create_dataset
    dsid = dataset.make_new_dset(self, shape, dtype, data, **kwds)
  File "/usr/lib/python2.7/dist-packages/h5py/_hl/dataset.py", line 88, in make_new_dset
    tid = h5t.py_create(dtype, logical=1)
  File "h5t.pyx", line 1332, in h5py.h5t.py_create (h5py/h5t.c:11600)
  File "h5t.pyx", line 1373, in h5py.h5t.py_create (h5py/h5t.c:11209)
  File "h5t.pyx", line 1311, in h5py.h5t._c_compound (h5py/h5t.c:10695)
  File "h5t.pyx", line 1332, in h5py.h5t.py_create (h5py/h5t.c:11600)
  File "h5t.pyx", line 1402, in h5py.h5t.py_create (h5py/h5t.c:11465)
TypeError: Object dtype dtype('object') has no native HDF5 equivalent

这有什么解决方法吗?

1 个答案:

答案 0 :(得分:6)

事实证明mat['ProteinComplex']的类型是一个scipy多维数组(ndarray)。可以使用ndarray.tolist()方法将这些转换为列表。如果我们将nd数组更改为列表,则在使用H5Py

创建数据集时它将起作用
>> mat = scipy.io.loadmat('state-10.mat')
>> h = h5py.File('test.hdf5','w')
>> protein_complex = mat['ProteinComplex'].tolist()
>> h.create_dataset('ProteinComplex', data=protein_complex)