如何在不覆盖的情况下写入hdf5文件?
How to write hdf5 files without overwriting?
抱歉,如果这是 h5py
上的一个非常基本的问题。
我正在阅读文档,但没有找到类似的示例。
我正在尝试使用 Python 创建多个 hdf5 数据集,但结果是在我关闭文件后数据将被覆盖。
假设我做了以下事情:
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()
我明白了
KeyError: "Unable to open object (Object 'data1' doesn't exist)"
如果我追加数据,需要先以 '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()
如果我在两种情况下都以 '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)
根据文档,数据应该连续存储,但我没有找到如何避免覆盖数据。
如何仅使用一条语句将数据存储在先前关闭的 hdf5 上?
如果你想在每个 运行 中创建一个唯一的文件,那么你应该考虑这样命名文件,例如将时间戳添加到文件名中,一个非常简单的例子将使用 datetime
模块和 now
和 strftime
方法来创建文件名。示例 -
import datetime
filename = "test_{}.hdf5".format(datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S"))
然后您可以使用该文件名打开文件。
演示 -
>>> 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'
抱歉,如果这是 h5py
上的一个非常基本的问题。
我正在阅读文档,但没有找到类似的示例。
我正在尝试使用 Python 创建多个 hdf5 数据集,但结果是在我关闭文件后数据将被覆盖。
假设我做了以下事情:
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()
我明白了
KeyError: "Unable to open object (Object 'data1' doesn't exist)"
如果我追加数据,需要先以 '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()
如果我在两种情况下都以 '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)
根据文档,数据应该连续存储,但我没有找到如何避免覆盖数据。
如何仅使用一条语句将数据存储在先前关闭的 hdf5 上?
如果你想在每个 运行 中创建一个唯一的文件,那么你应该考虑这样命名文件,例如将时间戳添加到文件名中,一个非常简单的例子将使用 datetime
模块和 now
和 strftime
方法来创建文件名。示例 -
import datetime
filename = "test_{}.hdf5".format(datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S"))
然后您可以使用该文件名打开文件。
演示 -
>>> 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'