我正在尝试使用 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()
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出:
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
[[ 1. 1. 1.]
[ 1. …Run Code Online (Sandbox Code Playgroud)