Python 处理(迭代)多个(HDF5)文件和每个HDF文件中的多个节点

Python 处理(迭代)多个(HDF5)文件和每个HDF文件中的多个节点,python,iteration,filenames,hdf5,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 table

如何在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)

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

我想,您问题的另一部分已经在中得到了回答(您可以使用
walkNodes
功能)。

问题到底是什么?如何自动完成?不是手动更改名称吗?您的代码编写不正确:如果您执行导入表的操作,则必须将此模块中的所有函数都写入表。函数。如果您编写
file=openFile(…)
new=creatArray(…)
它将无法工作!要在编写代码时使用该代码,您必须调用模块作为
从表导入openFile、creatArray
从表导入*
。在本例中,我使用库来处理HDF5文件。还有其他的图书馆你可以使用。我会给你一个建议。。。当您想知道函数具体在做什么以及它返回什么时,请键入
help(function)
。例如,要了解os.path.join正在做什么,您应该编写
帮助(os.path.join)
,您将得到:连接两个或多个路径名组件,根据需要插入“\”。如果任何组件是绝对路径,所有以前的路径组件都将被丢弃。非常感谢您的所有回答和建议!
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()