【问题标题】:Failing to write in hdf5 file无法写入 hdf5 文件
【发布时间】:2020-07-24 12:03:50
【问题描述】:

我正在尝试创建 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()

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

谁能指出我在这里遗漏了什么,非常感谢!

【问题讨论】:

  • 仔细检查 h5py 文档。每行有 2 个特殊类型变量,我认为每行限制为 1 个。

标签: python numpy hdf5 h5py


【解决方案1】:

不确定如何使用 h5py 解决此问题,但如果您未绑定到特定库,请查看 HDFql,因为使用它处理 HDF5 文件真的很容易。

在 Python 中使用 HDFql,您的用例可以在 hyperslabs 的帮助下解决,如下所示:

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。

【讨论】:

    【解决方案2】:

    您的代码适用于我(在 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 数据集。

    【讨论】:

      猜你喜欢
      • 2019-04-01
      • 2013-06-10
      • 2022-07-07
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 2022-10-17
      • 2021-06-08
      • 2014-06-20
      相关资源
      最近更新 更多