将几个hdf5文件合并到一个pytable中

时间:2013-10-01 13:02:12

标签: hdf5 pytables

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

我的意思是,如果file1中的数组的大小为x且file2中的数组的大小为y,则pytable中的结果数组的大小为x + y,其中包含来自file1的所有条目和然后是file2的所有条目。

1 个答案:

答案 0 :(得分:4)

您希望如何执行此操作略微取决于您拥有的数据类型。数组和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)