如何在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发布于 2012-05-11 00:14:25
如果一个目录中有多个文件,则可以使用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函数)。
https://stackoverflow.com/questions/10518267
复制相似问题