无法写入 hdf5 文件

我正在尝试创建 hdf5 文件,但输出文件为空。


我编写了一个 python 代码,它应该循环运行并在创建的数据集中写入字符串。文件保存后,我发现输出文件总是空的。


下面是我写的一段代码:


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

如上所述,输出文件为空。


谁能指出我在这里缺少什么,非常感谢!


侃侃尔雅
浏览 140回答 2
2回答

牛魔王的故事

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

UYOU

不确定如何使用 h5py 解决此问题,但如果您未绑定到特定库,请查看HDFql,因为使用它处理 HDF5 文件非常容易。在 Python 中使用 HDFql,您的用例可以在 hyperslabs 的帮助下解决,如下所示:import HDFqlHDFql.execute("CREATE AND USE FILE sample.h5")HDFql.execute("CREATE CHUNKED(1) DATASET objects/D1 AS VARCHAR(10, 2)")HDFql.execute("CREATE CHUNKED(1) DATASET objects/D2 AS VARCHAR(10, 3)")for i in range(10):&nbsp; &nbsp; HDFql.execute("INSERT INTO objects/D1(%d:::1) VALUES(Sample, %d)" % (i, i))&nbsp; &nbsp; HDFql.execute("INSERT INTO objects/D2(%d:::1) VALUES(Hello, World, %d)" % (i, i))HDFql.execute("CLOSE FILE")可以在此处找到有关如何使用 HDFql 的其他示例。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python