繁体   English   中英

h5py 如何将数据集移动到新组 - 奇怪的错误

[英]h5py how to move dataset to a new group - strange error

我有一个 hdf5 文件,它的数据集“foo”位于根目录。 我想将它移到一个新的组“栏”中。 我有RTFM并尝试了以下操作:

with h5py.File(fname, 'a') as h5file:
    newgroup = h5file.create_group('bar')
    h5file.move('foo', 'bar/foo')

这似乎工作正常。 现在我尝试实际验证它是否有效

with h5py.File(fname, 'r') as h5file:
    print(h5file['bar']['foo'].shape)

这实际上打印了数据集的正确形状。 但紧接着它抛出异常KeyError: "Unable to open object (object 'foo' doesn't exist)" 或者,更准确地说,

/opt/anaconda3/envs/py36qt5/lib/python3.6/site-packages/h5py/_hl/group.py in __getitem__(self, name)
    165                 raise ValueError("Invalid HDF5 object reference")
    166         else:
--> 167             oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
    168 
    169         otype = h5i.get_type(oid)

知道出了什么问题吗? h5py.__version__ = 2.7.1

编辑

这是一个更愚蠢的观察。 以下代码按预期工作,不会抛出上述异常。

with h5py.File(fname, 'r') as h5file:
    for key in h5file['bar'].keys():
        print(key, h5file['bar'][key].shape)

我完全不知所措......

我正在运行h5py 2.10 ,这段代码运行正常。
我不知道为什么它对你不起作用。

import h5py
import numpy as np

fname='SO_64558399.h5'

with h5py.File(fname, 'w') as h5file:
    arr = np.arange(100).reshape(10,10)
    h5file.create_dataset('foo',data=arr)
    
with h5py.File(fname, 'a') as h5file:
    newgroup = h5file.create_group('bar')
    h5file.move('foo', 'bar/foo')
    
with h5py.File(fname, 'r') as h5file:
    print(h5file['bar']['foo'].shape)
    for key in h5file['bar'].keys():
        print(key, h5file['bar'][key].shape)

打印出来的 output 是:

(10, 10)
foo (10, 10)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM