在每个HDF-fi中处理(迭代)多个(HDF5)文件和多个节点

2024-06-25 22:38:53 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在Numpy中自动处理升序文件名和数组名:

我有一系列名为:

20120101.hdf5, 20120102.hdf5, 20120103.hdf5, ..., 20120130.hdf5, 20120131.hdf5  

每个hdf5文件都包含几个数组,命名为:

^{pr2}$

我想分别修改每个数组,然后创建相应的新hdf5文件。例如,使用20120101.hdf5

import numpy
import tables

file = openFile("20120101.hdf5","r")
b1 = file.root.array1
c1 = (b1<=1)
new20120101_array1 = creatArray('/','1',c1)
c2 = ((b1<=2) and (b>1))
new20120101_array1 = creatArray('/','2',c2)
.
.
.

c20 = ((b1<=20) and (b>19))
new20120101_array1 = creatArray('/','20',c20)

对阵列2-24重复此操作。因此,我希望:

new20120101.hdf5 ---- new20120101_array1 ---- 1
                                              2
                                              ...
                                              20
                 ---- new20120101_array2 ---- 1
                                              ...
                                              20
                 ...
                 ---- new20120101_array24 --- 1
                                              ...
                                              20
new20120102.hdf5
....
new20120131.hdf5

Tags: and文件importnumpy数组b1filehdf5
1条回答
网友
1楼 · 发布于 2024-06-25 22:38:53

如果一个目录中有多个文件,可以使用os.listdir函数,该函数返回一个包含目录项名称的列表。在

示例:

import os
import tables

direc = '/Users/cg/' # the working directory (where your files are stored)
dirs = os.listdir(direc)

for idir in dirs: # this will iterate over the files in your working directory

    if idir.endswith('.he5'): # only for HDF5 files...
        hdf5 = tables.openFile(os.path.join(direc,idir))

        #### DO WHAT YOU WANT WITH EACH FILE!

        hdf5.close()

我想你问题的另一部分已经在你的other question中得到了回答(你可以使用walkNodes函数)。在

相关问题 更多 >