[英]Only the last saved dataset exists when using h5py
I am trying to save several datasets into a hdf5 file by h5py module, but it seems only the last one is saved.我正在尝试通过 h5py 模块将几个数据集保存到 hdf5 文件中,但似乎只保存了最后一个。 I think that because when a break statement was added, the first dataset is saved instead.我认为这是因为添加了 break 语句时,而是保存了第一个数据集。
The code in problem is below.问题代码如下。 How can I fix it?我该如何解决?
set_num = 0
for cur in data["init"]:
'''
got result as a list
'''
ipt = h5py.File(output_file, "w")
s = str(set_num)
ipt[s] = result
'''
create an attribute for ipt[s]
'''
set_num += 1
ipt.close()
#break
I apologize if there's any silly mistake.如果有任何愚蠢的错误,我深表歉意。
You are opening, closing the file on each pass of the for:
loop, and you have the attribute set to "w"
, meaning that it is overwriting the existing file during each pass.您在for:
循环的每次传递中打开、关闭文件,并且您将属性设置为"w"
,这意味着它在每次传递期间都会覆盖现有文件。
My recommendation is to instead open the file using the with
clause, and nest the for:
loop inside of that, which would make the intent clearer and obviate the need to explicitly close the file.我的建议是使用with
子句打开文件,并在其中嵌套for:
循环,这将使意图更清晰,并且无需显式关闭文件。 This example might help (though it is not tested, so may need modifications):这个例子可能会有所帮助(虽然它没有经过测试,所以可能需要修改):
with h5py.File(output_file, "w") as ipt:
for set_num, curr in enumerate(data["init"]):
s = str(set_num)
ipt[s] = result
You only get the last dataset because you are opening the file in write mode ('w')
, inside your loop.您只能获得最后一个数据集,因为您在循环内以写入模式('w')
打开文件。 Simple solution is use append mode ( a')
.简单的解决方案是使用附加模式( a')
。 Better, move the file open outside the loop and use the with...as:
context manager.更好的是,将文件移到循环外打开并使用with...as:
上下文管理器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.