发布网友 发布时间:2022-05-02 00:35
共1个回答
热心网友 时间:2022-04-18 16:15
用生成器
# 迭代目录下每一个文件
def iter_dir(path):
....for file in os.listdir(path):
........yield ...
# 迭代excel中工作表?行?
def iter_excel(file):
....with xlrd.open_workbook(file) as wb:
........for ....
............yield ...
#同时迭代两个目录中的文件与Excel中某对象
def myiter(path1,path2,excelfile):
....iter1,iter2,iter3=iter_dir(path1),iter_dir(path2),iter_excel(excelfile)
....while True:
........item = [next(iter1),next(iter2),next(iter3)]
........if item[0] and item[1] and item[2]:
............yield item
........else:
............raise StopIteration
#然后就可以用myiter去做循环了:
for file1,file2,xls in myiter('d:/source','d:/dest','d:/aa.xls'):
....想做了点什么就去做吧