我正在尝试使用 python 切片对象使用h5py
模块访问 HDF5 文件中的数据。我把这个例子放在一起来说明它适用于numpy
数组,但不适用于h5py
.
import h5py
import numpy as np
slice_obj = [slice(None,3,None), slice(2,5,None)]
test_array = np.ones((3,5))
print test_array[0:3,2:5]
print test_array[slice_obj]
f = h5py.File("testing.hdf5","w")
f['data'] = test_array
f.close()
f = h5py.File("testing.hdf5","r")
test2 = f['data'][0:3,2:5]
print test2
test2 = f['data'][slice_obj]
print test2
f.close()
这给出了以下输出:
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
Traceback (most recent call last):
File "slice.py", line 17, in <module>
test2 = f['data'][slice_obj]
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/h5py/_hl/dataset.py", line 439, in __getitem__
self.id.read(mspace, fspace, arr, mtype)
File "h5d.pyx", line 179, in h5py.h5d.DatasetID.read (h5py/h5d.c:2479)
File "_proxy.pyx", line 118, in h5py._proxy.dset_rw (h5py/_proxy.c:1300)
File "_proxy.pyx", line 84, in h5py._proxy.H5PY_H5Dread (h5py/_proxy.c:1051)
IOError: can't read data (Dataset: Read failed)
有谁知道这是否不可能h5py
?如果不是,是否有另一种方法来切片h5py
,使用对象或变量,而不是像f['data'][0:3,2:5]
我的示例中那样显式键入切片?