Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将新数据添加到HDF5文件会导致空数组_Python_Hdf5_H5py - Fatal编程技术网

Python 将新数据添加到HDF5文件会导致空数组

Python 将新数据添加到HDF5文件会导致空数组,python,hdf5,h5py,Python,Hdf5,H5py,在使用Python的HDF5包时,我发现了一个奇怪的行为。我想在表中插入更多数据。但不知怎的,我不能让它正常工作。正如您从源代码中看到的,我使用fromRow=hf[“X”]获取键“X”中的最后一行数据。shape[0]并随后写入tempArray2。结果是一个空表 import h5py tempArray1 = [[0.9293237924575806, -0.32789671421051025, 0.18110771477222443], [0.9293237924575806, -0.

在使用Python的HDF5包时,我发现了一个奇怪的行为。我想在表中插入更多数据。但不知怎的,我不能让它正常工作。正如您从源代码中看到的,我使用
fromRow=hf[“X”]获取键“X”中的最后一行数据。shape[0]
并随后写入
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数据集的大小后,您将从ROW中获得
。在调整大小之前,您需要该值。请参阅下面的代码

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