python - 处理(迭代)多个 (HDF5) 文件以及每个 HDF 文件中的多个节点

标签 python iteration filenames hdf5

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

我有一系列 HDF5 文件,名为:

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

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

array1, array2, array3, ..., array24

我想分别修改每个数组,然后创建相应的新 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

最佳答案

如果目录中有多个文件,则可以使用 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 函数)。

关于python - 处理(迭代)多个 (HDF5) 文件以及每个 HDF 文件中的多个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10518267/

相关文章:

python - 打印迭代次数而不换行

bash:迭代目录内容直到条件匹配的最佳实践

python - Django:django-tables2 更改默认排序行为

python - 如果路由以 'post/' 开头,Django 与 CreateView 不匹配

python - 在 Django 中获取彼此相邻的两个对象

Python - 有一个具有相同变量的子循环

python - 如何结合 None 和 "g"命名空间?

visual-studio-2008 - 具有多个同名源文件的 Visual Studio 项目?

c# - 来自字符串(路径)的文件名,不存在文件

java - 安卓/Java : filenames of contents raw folder to array