numpy 无法写入hdf5文件

hec6srdp  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(99)

我试图创建hdf 5文件,但输出文件是空的。
我写了一个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()

如上所述,输出文件为空。
有没有人能指出我在这里错过了什么,提前感谢!

zlwx9yxi

zlwx9yxi1#

你的代码为我工作(在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]d1view,但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数据集。

eaf3rand

eaf3rand2#

不知道如何使用h5py解决这个问题,但如果你没有绑定到特定的库,看看HDFql,因为它很容易处理HDF5文件。
在Python中使用HDFql,您的用例可以在hyperlabs的帮助下解决,如下所示:

import HDFql

HDFql.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):

    HDFql.execute("INSERT INTO objects/D1[%d:::1] VALUES(Sample, %d)" % (i, i))

    HDFql.execute("INSERT INTO objects/D2[%d:::1] VALUES(Hello, World, %d)" % (i, i))

HDFql.execute("CLOSE FILE")

有关如何使用HDFql的其他示例可以在here中找到。

相关问题