无法写入hdf5文件 [英] Failing to write in hdf5 file

查看:121
本文介绍了无法写入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)对于我在范围(10)中:d1 [0] [i] ='样本'd1 [1] [i] = str(i)d2 [0] [i] ='你好'd2 [1] [i] ='世界'd2 [2] [i] = str(i)hf.close() 

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

任何人都可以指出我在这里想念的是什么,谢谢!!

解决方案

您的代码对我有用(在ipython会话中):

 在[1]中:导入h5py在[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)...:对于范围在(10)中的我:...:d1 [0] [i] ='样本'...:d1 [1] [i] = str(i)...:d2 [0] [i] ='Hello'...:d2 [1] [i] =世界"...:d2 [2] [i] = str(i)...:hf.close() 

这将运行并创建一个文件.在正常情况下,它不是空"的.但是,如果文件为空,您是说它没有将单词写到文件中吗?出现的只是原始的''.

 在[4]中:hf = h5py.File(h5_file_name,'r')在[5]中:hf ['Objects/D1']Out [5]:< HDF5数据集"D1":形状(2、10),键入"| O">在[6]中:hf ['Objects/D1'] [:]出[6]:大批([['', '', '', '', '', '', '', '', '', ''],['','','','','','','','','','']]],dtype = object) 

===

问题不在于文件设置,而在于您尝试设置元素的方式:

 在[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)...:在[46]:d1 [:]出[46]:大批([['', '', '', '', '', '', '', '', '', ''],['','','','','','','','','','']]],dtype = object)在[47]中:d1 [0] [0] ='样本'在[48]:d1 [:]出[48]:大批([['', '', '', '', '', '', '', '', '', ''],['','','','','','','','','','']]],dtype = object) 

使用 tuple 索引风格:

 在[49]中:d1 [0,0] ='样本'在[50]:d1 [:]出[50]:array([['sample','','','','','','','','',''],['','','','','','','','','','']]],dtype = object) 

使用numpy数组 d1 [0] [0] = ... 可行,但这是因为 d1 [0] view d1 ,但 h5py (显然)并没有完全复制它. d1 [0] 是一个副本,是一个实际的numpy数组,而不是数据集本身.

关于整个数组索引的变化:

 在[51]中:d1 [0,:] ='sample'在[52]中:d1 [1,:] = np.arange(10)在[53]:d1 [:]出[53]:array([[''sample','sample','sample','sample','sample','sample','样品','样品','样品','样品'],['0','1','2','3','4','5','6','7','8','9']],dtype = object)在[54]中:d2 [:,0] = ['一个','两个','三个']在[55]:d2 [:]出[55]:array([[''one','','','','','','','','',''],['二', '', '', '', '', '', '', '', '', ''],['three','','','','','','','','','']],dtype = object) 

通过索引验证类型更改:

 在[64]中:type(d1)Out [64]:h5py._hl.dataset.Dataset在[65]中:type(d1 [0])出[65]:numpy.ndarray 

d1 [0] [0] ='foobar'将更改该 d1 [0] 数组,而不会影响 d1 数据集./p>

I am trying to create hdf5 file, but the output file is empty.

I have written a python code which is supposed to run in loop and write string in the created datasets. After the file gets saved, I found that the output file is always empty.

Below is the piece of code I have written:

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

The output file is empty as mentioned above.

Can anyone please point out what am I missing here, many thanks in advance !

解决方案

Your code works for me (in an ipython session):

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

This runs, and creates a file. It is not "empty" in the normal sense. But if by file being empty you mean that it didn't write the words to the file? All that's present is the original ''.

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)

===

The problem isn't with the file setup, but rather with how you are trying to set elements:

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)

Use the tuple style of indexing:

In [49]: d1[0, 0] = 'sample'                                                                           
In [50]: d1[:]                                                                                         
Out[50]: 
array([['sample', '', '', '', '', '', '', '', '', ''],
       ['', '', '', '', '', '', '', '', '', '']], dtype=object)

With a numpy array d1[0][0]=... works, but that's because d1[0] is a view of d1, but h5py (apparently) does not quite replicate this. d1[0] is a copy, an actual numpy array, not the dataset itself.

Variations on that whole-array indexing:

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)

Verifying the change in type with indexing:

In [64]: type(d1)                                                                                      
Out[64]: h5py._hl.dataset.Dataset
In [65]: type(d1[0])                                                                                   
Out[65]: numpy.ndarray

d1[0][0]='foobar' would change that d1[0] array without affecting the d1 dataset.

这篇关于无法写入hdf5文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆