将新数据添加到 HDF5 文件中会导致一个空数组
Adding new data into HDF5 file results an empty array
在玩 Python 的 HDF5 包时,我发现了一个奇怪的行为。我想在 table 中插入更多数据。但不知何故我无法让它正常工作。正如您从源代码中看到的那样,我使用 fromRow = hf["X"].shape[0]
获取键 'X' 中的最后一行数据,然后写入 tempArray2
。结果为空 table.
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
,代表现有 table 的结尾,它起作用了。
输出:
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
。在调整大小之前,您需要该值。请参阅下面的代码。
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])
# new location to get fromRow:
fromRow = 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])
hf["X"][fromRow:] = tempArray2
在玩 Python 的 HDF5 包时,我发现了一个奇怪的行为。我想在 table 中插入更多数据。但不知何故我无法让它正常工作。正如您从源代码中看到的那样,我使用 fromRow = hf["X"].shape[0]
获取键 'X' 中的最后一行数据,然后写入 tempArray2
。结果为空 table.
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
,代表现有 table 的结尾,它起作用了。
输出:
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
。在调整大小之前,您需要该值。请参阅下面的代码。
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])
# new location to get fromRow:
fromRow = 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])
hf["X"][fromRow:] = tempArray2