[英]Adding new data into HDF5 file results an empty array
在玩 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
奇怪的是,當我用數字 10 替換值fromRow
,例如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
。 您需要在調整大小之前的值。 請參閱下面的代碼。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.