您的代码适用于我(在 ipython 会话中):
In [1]: import h5py
In [2]: h5_file_name = 'sample.h5'
...: hf = h5py.File(h5_file_name, 'w')
...: g1 = hf.create_group('Objects')
...: dt = h5py.special_dtype(vlen=str)
...: d1 = g1.create_dataset('D1', (2, 10), dtype=dt)
...: d2 = g1.create_dataset('D2', (3, 10), dtype=dt)
...: for i in range(10):
...: d1[0][i] = 'Sample'
...: d1[1][i] = str(i)
...: d2[0][i] = 'Hello'
...: d2[1][i] = 'World'
...: d2[2][i] = str(i)
...: hf.close()
这会运行并创建一个文件。它不是正常意义上的“空”。但是,如果文件为空,则意味着它没有将单词写入文件?现在的所有内容都是原始的''。
In [4]: hf = h5py.File(h5_file_name, 'r')
In [5]: hf['Objects/D1']
Out[5]: <HDF5 dataset "D1": shape (2, 10), type "|O">
In [6]: hf['Objects/D1'][:]
Out[6]:
array([['', '', '', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', '', '', '']], dtype=object)
===
问题不在于文件设置,而在于您尝试设置元素的方式:
In [45]: h5_file_name = 'sample.h5'
...: hf = h5py.File(h5_file_name, 'w')
...: g1 = hf.create_group('Objects')
...: dt = h5py.special_dtype(vlen=str)
...: d1 = g1.create_dataset('D1', (2, 10), dtype=dt)
...: d2 = g1.create_dataset('D2', (3, 10), dtype=dt)
...:
In [46]: d1[:]
Out[46]:
array([['', '', '', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', '', '', '']], dtype=object)
In [47]: d1[0][0] = 'sample'
In [48]: d1[:]
Out[48]:
array([['', '', '', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', '', '', '']], dtype=object)
使用tuple 风格的索引:
In [49]: d1[0, 0] = 'sample'
In [50]: d1[:]
Out[50]:
array([['sample', '', '', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', '', '', '']], dtype=object)
使用 numpy 数组 d1[0][0]=... 可以工作,但那是因为 d1[0] 是 d1 的 view,但 h5py(显然)并没有完全复制这一点。 d1[0] 是一个副本,一个实际的 numpy 数组,而不是数据集本身。
整个数组索引的变化:
In [51]: d1[0, :] = 'sample'
In [52]: d1[1, :] = np.arange(10)
In [53]: d1[:]
Out[53]:
array([['sample', 'sample', 'sample', 'sample', 'sample', 'sample',
'sample', 'sample', 'sample', 'sample'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']], dtype=object)
In [54]: d2[:,0] = ['one','two','three']
In [55]: d2[:]
Out[55]:
array([['one', '', '', '', '', '', '', '', '', ''],
['two', '', '', '', '', '', '', '', '', ''],
['three', '', '', '', '', '', '', '', '', '']], dtype=object)
使用索引验证类型的变化:
In [64]: type(d1)
Out[64]: h5py._hl.dataset.Dataset
In [65]: type(d1[0])
Out[65]: numpy.ndarray
d1[0][0]='foobar' 将更改 d1[0] 数组而不影响 d1 数据集。