如何使用pytables或h5py将数据集对象复制到其他hdf5文件? [英] How to copy a dataset object to a different hdf5 file using pytables or h5py?

查看:512
本文介绍了如何使用pytables或h5py将数据集对象复制到其他hdf5文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我选择了特定的hdf5数据集,并希望将其复制到新的hdf5文件中.我可以找到一些有关在两个文件之间进行复制的教程,但是如果您刚刚创建了一个新文件,又想将数据集复制到该文件,该怎么办?我以为下面的方法行得通,但事实并非如此.有没有简单的方法可以做到这一点?

I have selected specific hdf5 datasets and want to copy them to a new hdf5 file. I could find some tutorials on copying between two files, but what if you have just created a new file and you want to copy datasets to the file? I thought the way below would work, but it doesn't. Are there any simple ways to do this?

>>> dic_oldDataset['old_dataset']
<HDF5 dataset "old_dataset": shape (333217,), type "|V14">

>>> new_file = h5py.File('new_file.h5', 'a')
>>> new_file.create_group('new_group')

>>> new_file['new_group']['new_dataset'] = dic_oldDataset['old_dataset']


RuntimeError: Unable to create link (interfile hard links are not allowed)

推荐答案

答案1(使用h5py):
这将创建一个简单的结构化数组,以填充第一个文件中的第一个数据集. 然后从该数据集中读取数据,并使用my_array复制到第二个文件.

Answer 1 (using h5py):
This creates a simple structured array to populate the first dataset in the first file. The data is then read from that dataset and copied to the second file using my_array.

import h5py, numpy as np

arr = np.array([(1,'a'), (2,'b')], 
      dtype=[('foo', int), ('bar', 'S1')]) 
print (arr.dtype)

h5file1 = h5py.File('test1.h5', 'w')
h5file1.create_dataset('/ex_group1/ex_ds1', data=arr)                
print (h5file1)

my_array=h5file1['/ex_group1/ex_ds1']

h5file2 = h5py.File('test2.h5', 'w')
h5file2.create_dataset('/exgroup2/ex_ds2', data=my_array)
print (h5file2)

h5file1.close()
h5file2.close()

这篇关于如何使用pytables或h5py将数据集对象复制到其他hdf5文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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