将几个hdf5文件合并到一个pytable中 [英] merging several hdf5 files into one pytable

查看:331
本文介绍了将几个hdf5文件合并到一个pytable中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have several hdf5 files, each of them with the same structure. I'd like to create one pytable out of them by somehow merging the hdf5 files.

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

What I mean is that if an array in file1 has size x and array in file2 has size y, the resulting array in the pytable will be of size x+y, containing first all the entries from file1 and then all the entries from file2.

推荐答案

此操作的方式在某种程度上取决于您拥有的数据类型.数组和CArray具有静态大小,因此您需要预先分配数据空间.因此,您将执行以下操作:

How you want to do this depends slightly on the data type that you have. Arrays and CArrays have a static size so you need to preallocate the data space. Thus you would do something like the following:

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[:]

但是,EArray和表是可扩展的.因此,您不需要预先分配大小,而可以使用copy_node()和append()代替.

However, EArrays and Tables are extendable. Thus you don't need to preallocate the size and can copy_node() and append() instead.

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)

这篇关于将几个hdf5文件合并到一个pytable中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆