4

我有几个hdf5文件,每个文件都具有相同的结构。我想pytable通过以某种方式合并hdf5文件来创建一个。

我的意思是,如果 file1 中的数组大小为 x,而 file2 中的数组大小为 y,则结果数组pytable的大小为 x+y,首先包含 file1 中的所有条目,然后包含 file2 中的所有条目。

4

1 回答 1

6

您希望如何执行此操作取决于您拥有的数据类型。数组和 CArray 具有静态大小,因此您需要预先分配数据空间。因此,您将执行以下操作:

import tables as tb
file1 = tb.open_file('/path/to/file1', 'r')
file2 = tb.open_file('/path/to/file2', 'r')
file3 = tb.open_file('/path/to/file3', 'r')
x = file1.root.x
y = file2.root.y

z = file3.create_array('/', 'z', atom=x.atom, shape=(x.nrows + y.nrows,))
z[:x.nrows] = x[:]
z[x.nrows:] = y[:]

但是,EArrays 和 Tables 是可扩展的。因此,您不需要预先分配大小,而是可以使用 copy_node() 和 append() 。

import tables as tb
file1 = tb.open_file('/path/to/file1', 'r')
file2 = tb.open_file('/path/to/file2', 'r')
file3 = tb.open_file('/path/to/file3', 'r')
x = file1.root.x
y = file2.root.y

z = file1.copy_node('/', name='x', newparent=file3.root, newname='z')
z.append(y)
于 2013-10-01T17:26:53.307 回答