python if循环在两个循环后停止,而没有到达第三个循环
我已经查看了其他问题,但没有找到解决方案。我需要脚本来遍历文件夹中的所有文件,并将数据放入相应的表中python if循环在两个循环后停止,而没有到达第三个循环,python,excel,if-statement,Python,Excel,If Statement,我已经查看了其他问题,但没有找到解决方案。我需要脚本来遍历文件夹中的所有文件,并将数据放入相应的表中 for fi in os.listdir('C_data'): file_nm = fi #fi=[] grp= (fi[:3]) if fi=="A_Pro_Template.xlsx": print 'Beginning file:', file_nm wb = open_workbook('C_data'+'\\'+file_
for fi in os.listdir('C_data'):
file_nm = fi
#fi=[]
grp= (fi[:3])
if fi=="A_Pro_Template.xlsx":
print 'Beginning file:', file_nm
wb = open_workbook('C_data'+'\\'+file_nm)
sh = wb.sheet_by_name('Pro')
grp2=(grp.lower())
values = []
for r in range(1,sh.nrows):
#if len(str(sh.cell(r,7).value))>0:
v=[None]*9
v[0]=str(sh.cell(r,0).value)[:6]
v[1]=sh.cell(r,2).value
v[2]=sh.cell(r,1).value[:10]
v[3]=sh.cell(r,3).value[:10]
v[4]=sh.cell(r,4).value[:30]
v[7]=time
v[8]=file_nm
values.append(tuple(v))
for i in lst_split(len(values)):
cnxn.cursor().executemany('insert into PROD."'+grp2+'ProX" values (%s,%s,%s,%s,%s,%s,%s,%s,%s)', values[min(i):max(i)+1])
if fi=="B_Pro_Template.xlsx":
print 'Beginning file:', file_nm
wb = open_workbook('C_data'+'\\'+file_nm)
sh = wb.sheet_by_name('Pro')# this works until I add instruction tab and then it loops 3 times
grp2=(grp.lower())
values = []
for r in range(1,sh.nrows):
#if len(str(sh.cell(r,7).value))>0:
v=[None]*14
v[0]= None
v[1]=str(sh.cell(r,0).value)[:6]
v[2]=sh.cell(r,2).value
v[3]=sh.cell(r,1).value[:10]
v[4]=None
v[5]=sh.cell(r,3).value[:10]
v[11]= None
v[12]=time
values.append(tuple(v))
for i in lst_split(len(values)):
cnxn.cursor().executemany('insert into PROD."'+grp2+'ProX"
values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',
values[min(i):max(i)+1])
if fi=="C_Pro_Template.xlsx":
print 'Beginning file:', file_nm
wb = open_workbook('C_data'+'\\'+file_nm)
sh = wb.sheet_by_name('Pro')
grp2=(grp.lower())
values = []
for r in range(1,sh.nrows):
#if len(str(sh.cell(r,7).value))>0:
v=[None]*14
v[0]= None
v[1]=str(sh.cell(r,0).value)[:6]
v[2]=sh.cell(r,2).value
v[3]=sh.cell(r,1).value[:10]
v[4]=None
v[9]=None
v[11]= None
v[12]=time
v[13]=file_nm
values.append(tuple(v))
for i in lst_split(len(values)):
cnxn.cursor().executemany('insert into PROD."'+grp2+'ProX"
values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',
values[min(i):max(i)+1])
我们的目标是让脚本在文件夹中的所有文件中运行,但脚本不会超过第二个文件。我可以看到,您使用的是两种类型的文件,数据不同
glob
允许您读取多个文件并将其存储为列表(您还可以指定读取时文件中的文本是否与特定字符串匹配)
然后,您可以定义两个函数来读取任一文件类型,并从读取文件列表中调用它们
以下是如何实现代码的提示:
def processxxfiles(文件):
#<处理“A_Pro_Template.xlsx”文件中数据的代码>
def processAfiles(文件):
#<处理“A_Pro_Template.xlsx”文件中数据的代码>
导入glob
目录='C_数据'
xxfiles=sorted(glob.glob(directory+'/*xx_Pro_Template.xlsx'))#这将读取目录文件名中包含“xx_Pro_Template”的所有xlsx文件,并将其保存到xxfiles列表中
Afiles=sorted(glob.glob(directory+'/*A_Pro_Template.xlsx'))
#然后使用for循环迭代每个文件类型的列表
对于xxx文件中的文件:
processxxfiles(文件)
对于文件中的文件:
processAfiles(文件)
欢迎来到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。'def processAAAfiles(文件):def processBBBfiles(文件):def processCCCfiles(文件):import glob您可以为每种类型的文件定义任意多个函数。然后为文件列表中的每个文件调用该函数。它对您有效吗?我收到了以下错误:AAAfiles=sorted(glob.glob(directory+'/*AAA_Pro_Template.xlsx'))name错误:名称“glob”未定义您是否忘记了“导入glob”?“导入日期时间导入操作系统从xlrd导入xlrd导入打开的工作簿def processADGfiles(文件):#def processCLGfiles(文件):#def processCSGfiles(文件):导入glob'我没有安装glob吗?谢谢
def processxxfiles(file):
#<code to process data in "A_Pro_Template.xlsx" files>
def processAfiles(file):
#<code to process data in "A_Pro_Template.xlsx" files>
import glob
directory = 'C_data'
xxfiles = sorted (glob.glob(directory + '/*xx_Pro_Template.xlsx')) # This will read all xlsx files with 'xx_Pro_Template' in the filename in the directory and save them to xxfiles list
Afiles = sorted (glob.glob(directory + '/*A_Pro_Template.xlsx'))
# Then use a for loop to iterate over the list of each file type
for file in xxfiles:
processxxfiles(file)
for file in Afiles:
processAfiles(file)