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