在使用h5py打开和写入h5文件时,是否清理了现有组或数据集

2024-05-16 23:49:10 发布

您现在位置:Python中文网/ 问答频道 /正文

一些数据需要以不同的步骤写入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 

Tags: and数据importdatacreatenp步骤open
1条回答
网友
1楼 · 发布于 2024-05-16 23:49:10

我认为模式“w”将始终创建一个新的HDF5文件,因此第二次必须以读/写/创建模式打开(“a”,用于附加):

#second open and write
b=h5py.File(a, 'a')

为我工作:

>>> list(b.keys())
['QC Attributes', 'QC Attributes xxxx']

相关问题 更多 >