向h5文件追加数据
import h5py
import numpy as np
import os
features = np.random.randn(3,3,3)
print(features)
result_path = "train_feature.h5"
def write_h5(feature,name):
if not os.path.exists(result_path):
file = h5py.File(result_path, "a")
else:
file = h5py.File(result_path, 'a')
file.create_dataset(name=name, data=feature)
file.close()
for i in range(0,3):
feat = features[i,:,:]
name = str(i)
write_h5(feat, name)
with h5py.File(result_path, 'r') as hf:
for key in hf.keys():
print(hf[key].name)
for idx in range(0, 3):
print(hf[str(idx)][:])
hf.close()