且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用pytables或h5py将数据集对象复制到其他hdf5文件?

更新时间:2022-12-08 13:46:56

答案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()