在使用Python的HDF5包时,我发现了一个奇怪的行为。我想在表中插入更多数据。但不知怎的,我不能让它正常工作。正如您从源代码中看到的,我使用fromRow = hf["X"].shape[0]
获取键“X”中的最后一行数据,然后写入tempArray2
。结果是一个空表
import h5py
tempArray1 = [[0.9293237924575806, -0.32789671421051025, 0.18110771477222443], [0.9293237924575806, -0.32789671421051025, 0.18110771477222443], [0.9293237924575806, -0.32789671421051025, 0.18110771477222443], [0.9293237924575806, -0.32789671421051025, 0.18110771477222443], [0.9293237924575806, -0.32789671421051025, 0.18110771477222443], [0.9293237924575806, -0.32789671421051025, 0.18110771477222443], [0.9293237924575806, -0.32789671421051025, 0.18110771477222443], [0.9293237924575806, -0.32789671421051025, 0.18110771477222443], [0.9293237924575806, -0.32789671421051025, 0.18110771477222443], [0.9293237924575806, -0.32789671421051025, 0.18110771477222443]]
tempArray2 = [[3.1387749004352372e-06, 8.120089097236803e+27, -1.645612730013634e-14], [3.1387749004352372e-06, 8.120089097236803e+27, -1.645612730013634e-14], [3.1387749004352372e-06, 8.120089097236803e+27, -1.645612730013634e-14], [3.1387749004352372e-06, 8.120089097236803e+27, -1.645612730013634e-14], [3.1387749004352372e-06, 8.120089097236803e+27, -1.645612730013634e-14], [3.1387749004352372e-06, 8.120089097236803e+27, -1.645612730013634e-14], [3.1387749004352372e-06, 8.120089097236803e+27, -1.645612730013634e-14], [3.1387749004352372e-06, 8.120089097236803e+27, -1.645612730013634e-14], [3.1387749004352372e-06, 8.120089097236803e+27, -1.645612730013634e-14], [3.1387749004352372e-06, 8.120089097236803e+27, -1.645612730013634e-14]]
with h5py.File('data.hdf5', 'w') as hf:
# Add data to new file
dset = hf.create_dataset("X", data=tempArray1, compression="gzip", chunks=True, maxshape=(None,3), dtype='f4') # Size is as the size of tempArray1
print(hf["X"].shape[0])
# Append data existing file
hf["X"].resize((hf["X"].shape[0] + 10, 3)) # Size is as the size of X+ 10
print(hf["X"].shape[0])
fromRow = hf["X"].shape[0]
hf["X"][fromRow:] = tempArray2
这就是它的样子:
Key: X
Data:
[[ 0.9293238 -0.3278967 0.18110771]
[ 0.9293238 -0.3278967 0.18110771]
[ 0.9293238 -0.3278967 0.18110771]
[ 0.9293238 -0.3278967 0.18110771]
[ 0.9293238 -0.3278967 0.18110771]
[ 0.9293238 -0.3278967 0.18110771]
[ 0.9293238 -0.3278967 0.18110771]
[ 0.9293238 -0.3278967 0.18110771]
[ 0.9293238 -0.3278967 0.18110771]
[ 0.9293238 -0.3278967 0.18110771]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]]
Length of data: 20
奇怪的是,当我将值fromRow
替换为数字10,比如fromRow = 10
,表示现有表的结尾时,它就工作了
输出:
Key: X
Data:
[[ 9.2932379e-01 -3.2789671e-01 1.8110771e-01]
[ 9.2932379e-01 -3.2789671e-01 1.8110771e-01]
[ 9.2932379e-01 -3.2789671e-01 1.8110771e-01]
[ 9.2932379e-01 -3.2789671e-01 1.8110771e-01]
[ 9.2932379e-01 -3.2789671e-01 1.8110771e-01]
[ 9.2932379e-01 -3.2789671e-01 1.8110771e-01]
[ 9.2932379e-01 -3.2789671e-01 1.8110771e-01]
[ 9.2932379e-01 -3.2789671e-01 1.8110771e-01]
[ 9.2932379e-01 -3.2789671e-01 1.8110771e-01]
[ 9.2932379e-01 -3.2789671e-01 1.8110771e-01]
[ 3.1387749e-06 8.1200891e+27 -1.6456127e-14]
[ 3.1387749e-06 8.1200891e+27 -1.6456127e-14]
[ 3.1387749e-06 8.1200891e+27 -1.6456127e-14]
[ 3.1387749e-06 8.1200891e+27 -1.6456127e-14]
[ 3.1387749e-06 8.1200891e+27 -1.6456127e-14]
[ 3.1387749e-06 8.1200891e+27 -1.6456127e-14]
[ 3.1387749e-06 8.1200891e+27 -1.6456127e-14]
[ 3.1387749e-06 8.1200891e+27 -1.6456127e-14]
[ 3.1387749e-06 8.1200891e+27 -1.6456127e-14]
[ 3.1387749e-06 8.1200891e+27 -1.6456127e-14]]
Length of data: 20
知道我做错了什么吗
调整X数据集的大小后,您将得到
fromRow
。在调整大小之前,您需要该值。请参阅下面的代码相关问题 更多 >
编程相关推荐