【发布时间】:2013-10-07 15:44:45
【问题描述】:
我有几个hdf5 文件,每个文件都具有相同的结构。我想通过某种方式合并hdf5 文件,从中创建一个pytable。
我的意思是,如果 file1 中的数组大小为 x,而 file2 中的数组大小为 y,则 pytable 中的结果数组大小为 x+y,首先包含来自 file1 的所有条目,然后包含所有条目file2 中的条目。
【问题讨论】:
我有几个hdf5 文件,每个文件都具有相同的结构。我想通过某种方式合并hdf5 文件,从中创建一个pytable。
我的意思是,如果 file1 中的数组大小为 x,而 file2 中的数组大小为 y,则 pytable 中的结果数组大小为 x+y,首先包含来自 file1 的所有条目,然后包含所有条目file2 中的条目。
【问题讨论】:
您希望如何执行此操作取决于您拥有的数据类型。数组和 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)
【讨论】:
file3时使用'a'而不是'r'。
z.append(y[:])