[英]How to write hdf5 files without overwriting?
Sorry if this is a very basic question on h5py
. 抱歉,这是关于
h5py
的非常基本的问题。
I was reading the documentation, but I didn't find a similar example. 我正在阅读文档,但没有找到类似的示例。
I'm trying to create multiple hdf5 datasets with Python, but it turns out after I close the file data will be overwritten. 我正在尝试使用Python创建多个hdf5数据集,但事实证明,关闭文件后,数据将被覆盖。
Let's say I do the following: 假设我执行以下操作:
import numpy as np
import h5py
f = h5py.File('test.hdf5', 'w')
f.create_dataset('data1', data = np.ones(10))
f.close()
f = h5py.File('test.hdf5', 'w')
f.create_dataset('data0', data = np.zeros(10))
f.close()
f = h5py.File('test.hdf5', 'r')
f["data1"].value
f.close()
I get 我懂了
KeyError: "Unable to open object (Object 'data1' doesn't exist)"
KeyError:“无法打开对象(对象'data1'不存在)”
If I append data, that requires first opening in 'w'
mode and then appending in 'a'
mode, having two different statements. 如果我追加数据,则需要先以
'w'
模式打开,然后以两个不同的语句以'a'
模式追加。
import numpy as np
import h5py
f = h5py.File('test.hdf5', 'w')
f.create_dataset('data1', data = np.ones(10))
f.close()
f = h5py.File('test.hdf5', 'a')
f.create_dataset('data0', data = np.zeros(10))
f.close()
f = h5py.File('test.hdf5', 'r')
f["data1"].value
f.close()
If I open the file in 'a'
mode in both cases: 如果在两种情况下都以
'a'
模式打开文件:
import numpy as np
import h5py
f = h5py.File('test.hdf5', 'a')
f.create_dataset('data1', data = np.ones(10))
f.close()
f = h5py.File('test.hdf5', 'a')
f.create_dataset('data0', data = np.zeros(10))
f.close()
f = h5py.File('test.hdf5', 'r')
print(f['data1'].value)
f.close()
RuntimeError: Unable to create link (Name already exists)
RuntimeError:无法创建链接(名称已存在)
According to the documentation, data should be stored contiguously, but I didn't find how to avoid overwriting data. 根据文档,数据应该连续存储,但是我没有找到如何避免覆盖数据的方法。
How can I store data on a previously closed hdf5 only using one single statement? 如何仅使用一条语句将数据存储在先前关闭的hdf5上?
If you want to create a unique file in each run, then you should consider naming the file like that , an example would be to add the timestamp to the name of the file, A very simply example would be to use datetime
module and now
and strftime
method to create the file name. 如果您想在每次运行中创建一个唯一的文件,则应考虑像这样命名该文件,例如将时间戳添加到文件名中。一个非常简单的示例是使用
datetime
模块, now
和strftime
方法创建文件名。 Example - 范例-
import datetime
filename = "test_{}.hdf5".format(datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S"))
Then you can use that filename to open the file. 然后,您可以使用该文件名打开文件。
Demo - 演示-
>>> import datetime
>>> filename = "test_{}.hdf5".format(datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S"))
>>> filename
'test_2015_08_09_13_33_43.hdf5'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.