一些数据需要以不同的步骤写入HDF5文件,示例代码如下。我遇到的问题是,再次执行新的步骤(包括打开和写入)时,将清理现有的h5组和数据集。
import h5py
import numpy as np
a=r"F:\HY1A1B\cd.h5"
#first open and write
b=h5py.File(a, 'w')
zeroPixelCounts = np.zeros((5,10))
QC_Attribute = b.create_group("QC Attributes")
QC_Attribute.create_dataset("Zero Pixel Counts",(5,10),data=zeroPixelCounts)
b.close()
#second open and write
b=h5py.File(a, 'w')
QC_Attributex = b.create_group("QC Attributes xxxx")
QC_Attributex.create_dataset("Zero Pixel Counts",(5,10),data=zeroPixelCounts)
b.close()
#problem:the existing data in first open and write processing were cleaned
答案 0 :(得分:0)
我认为模式“ w”将始终创建一个新的HDF5文件,因此第二次您必须以读取/写入/创建模式打开(“ a”,用于附加):
#second open and write
b=h5py.File(a, 'a')
为我工作:
>>> list(b.keys())
['QC Attributes', 'QC Attributes xxxx']