如何使用python将Hdf5文件部分复制到一个保持相同结构的新文件中? [英] How to partially copy using python an Hdf5 file into a new one keeping the same structure?
问题描述
我有一个很大的hdf5文件,看起来像这样:
I have a large hdf5 file that looks something like this:
A/B/dataset1, dataset2
A/C/dataset1, dataset2
A/D/dataset1, dataset2
A/E/dataset1, dataset2
...
我只想创建一个新文件: A/B/数据集1,数据集2 A/C/dataset1,dataset2
I want to create a new file with only that: A/B/dataset1, dataset2 A/C/dataset1, dataset2
在python中最简单的方法是什么?
What is the easiest way in python?
我做到了:
fs = h5py.File('source.h5', 'r')
fd = h5py.File('dest.h5', 'w')
fs.copy('group B', fd)
问题是我得到了dest.h5:
the problem is that I get for dest.h5:
B/dataset1, dataset2
并且我缺少部分乔木.
推荐答案
fs.copy('A/B', fd)
不会将路径/A/B/
复制到fd
,它只会复制组B
(如您所知! ).因此,您首先需要创建路径的其余部分:
fs.copy('A/B', fd)
doesn't copy the path /A/B/
into fd
, it only copies the group B
(as you've found out!). So you first need to create the rest of the path:
fd.create_group('A')
fs.copy('A/B', fd['/A'])
或者,如果您将经常使用该网上论坛:
or, if you will be using the group a lot:
fd_A = fd.create_group('A')
fs.copy('A/B', fd_A)
这会将组B
从fs['/A/B']
复制到fd['/A']
:
In [1]: fd['A/B'].keys()
Out[1]: [u'dataset1', u'dataset2']
这是自动执行此操作的方法:
Here's an automatic way of doing this:
# Get the name of the parent for the group we want to copy
group_path = fs['/A/B'].parent.name
# Check that this group exists in the destination file; if it doesn't, create it
# This will create the parents too, if they don't exist
group_id = fd.require_group(group_path)
# Copy fs:/A/B/ to fd:/A/G
fs.copy('/A/B', group_id, name="G")
print(fd['/A/G'].keys())
# [u'dataset1', u'dataset2']
这篇关于如何使用python将Hdf5文件部分复制到一个保持相同结构的新文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!