mardi 4 décembre 2018

python if loop stopping after 2 loops and not getting to third

I have looked at the other questions and do not see a solution.I need the script to just go through all the files in the folder and put the data in the appropriate tables.

for fi in os.listdir('C_data'):
    file_nm = fi
    #fi=[]
    grp= (fi[:3])
    if fi=="xx_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=="A_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])

The goal is to have the script run through all the files in the folder but the script is not reaching past the 2nd file.

Aucun commentaire:

Enregistrer un commentaire