簡體   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