【发布时间】: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())