将 hdf5 附加到另一个 hdf5 文件
Posted
技术标签:
【中文标题】将 hdf5 附加到另一个 hdf5 文件【英文标题】:Append hdf5 to another hdf5 file 【发布时间】:2020-09-13 09:26:45 【问题描述】:我有几个具有相同形状的 hdf5 文件,包含 x 和 y 列。 我需要附加这些,以获得一个包含所有数据的 hdf5 文件。
到目前为止我的代码:
def append_to_h5(new_file, file_list):
f = h5py.File(new_file, 'a')
for file in file_list:
with h5py.File(file, 'r') as d:
f.create_dataset("./", data=d)
f.close()
#new_file <- is a file path to the new hdf5 file
#file_list <- contains all the pathes of the hdf5 files, which I want to append
错误
in make_new_dset tid = h5t.py_create(dtype, logical=1)
File "h5py/h5t.pyx", line 1634, in h5py.h5t.py_create
File "h5py/h5t.pyx", line 1656, in h5py.h5t.py_create
File "h5py/h5t.pyx", line 1717, in h5py.h5t.py_create
TypeError: No conversion path for dtype: dtype('<U1')
感谢任何想法 谢谢
【问题讨论】:
您的代码没有意义。d
是打开的文件。 create_dataset
用于创建(和写入)一个数组。它不能用于将整个文件甚至一个组复制到新文件中。我认为您需要花更多时间阅读h5py
文档。
解决方案取决于您希望如何处理来自每个 HDF5 文件的数据集中的数据。例如,您是否要将数据集复制到通用 HDF5 文件中的相同数据集名称(并且它们具有唯一名称)?还是您想从每个数据集/文件中提取数据并附加到公共文件中的单个数据集?您是否考虑过外部链接?查看此答案以查看 4 种不同方法的评论:SO 10462884。使用 pytables 也有答案。
另外,正如 hpaulj 所指出的,您的内部循环(在 d
上)在文件名列表中循环。每个文件至少需要 1 个嵌套循环才能在根级别的数据集上循环(使用 d.keys()
)
【参考方案1】:
这在其他 SO 答案中得到了更广泛的介绍。我创建了一个简短的示例来帮助您入门。主要变化是添加一个循环来查找和复制***数据集(仅限)。它假定不会有数据集名称冲突,并且需要将测试用于通用案例。另外,我更改了您的文件对象变量名称。
def append_to_h5(new_file, file_list):
f1 = h5py.File(new_file, 'a')
for file in file_list:
with h5py.File(file, 'r') as f2:
for ds in f2.keys():
f2.copy(ds, f1)
f1.close()
#new_file <- is a file path to the new hdf5 file
#file_list <- contains all the pathes of the hdf5 files, which I want to append
【讨论】:
以上是关于将 hdf5 附加到另一个 hdf5 文件的主要内容,如果未能解决你的问题,请参考以下文章
编写并将float数组附加到C ++中hdf5文件中的唯一数据集
使用 Pandas、Python 将数据附加到 HDF5 文件