簡體   English   中英

For 循環計數器正在內部 while 循環中重置。 在 while 循環中顯示一個固定值而不是一個變化的值

[英]For loop counter is resetting in an internal while loop. Showing a fixed value in while loop and not a changing one

代碼的想法是首先從 excel 表中獲取一些數據並對其進行修改,然后將其與文本文檔進行交叉比較。 下面的代碼與其下面的代碼一起繼續。

import xlsxwriter

workbook1 = xlsxwriter.Workbook('Imports.xlsx')
worksheet1 = workbook1.add_worksheet()
workbook2 = xlsxwriter.Workbook('Modified.xlsx')
worksheet2 = workbook2.add_worksheet()

workbook1.close()
workbook2.close()

import pandas as pd
import openpyxl
from openpyxl.styles import Font
import re
    
df1 = pd.read_excel('Employees.xlsx', sheet_name='Sheet1', usecols="B",)

writer=pd.ExcelWriter('Imports.xlsx')

# Writing dataframes into columns
df1.to_excel(writer,index=False,startcol=0)
writer.save()

wb = openpyxl.load_workbook("Imports.xlsx")
ws = wb["Sheet1"]

i = 0
print("Text replacement part 1 of 4")
for r in range(1,ws.max_row+1):
    for c in range(1,ws.max_column+1):
        s = ws.cell(r,c).value 
        if s != None and re.search('^Mr ',s,flags=re.I): 
                ws.cell(r,c).value = re.sub('^Mr ','',s,flags=re.I)
                
        if s != None and re.search('^Ms ',s,flags=re.I): 
                ws.cell(r,c).value = re.sub('^Ms ','',s,flags=re.I)
                
        if s != None and re.search('^Mrs ',s,flags=re.I): 
                ws.cell(r,c).value = re.sub('^Mrs ','',s,flags=re.I)
                
        if s != None and re.search('^Miss ',s,flags=re.I): 
                ws.cell(r,c).value = re.sub('^Miss ','',s,flags=re.I)
                
        if s != None and re.search('^s ',s,flags=re.I): 
                ws.cell(r,c).value = re.sub('^s ','',s,flags=re.I)                

# Text replacement for list so names in are same format
print("Text replacement part 2 of 4")
for r in range(1,ws.max_row+1):
    for c in range(1,ws.max_column+1):
        s = ws.cell(r,c).value                
        if s != None and re.search(' \(S\)$',s,flags=re.I): 
                ws.cell(r,c).value = re.sub(' \(S\)','',s,flags=re.I)
                
        if s != None and re.search(' \(F\)$',s,flags=re.I): 
                ws.cell(r,c).value = re.sub(' \(F\)','',s,flags=re.I)
                
          
# Text replacement for list so names in are same format
print("Text replacement part 3 of 4") 
for r in range(1,ws.max_row+1):
    for c in range(1,ws.max_column+1):
        s = ws.cell(r,c).value                      
        if s != None and re.search('  ',s,flags=re.I): 
                ws.cell(r,c).value = re.sub('  ',' ',s,flags=re.I)  
                
for r in range(1,ws.max_row+1):
    for c in range(1,ws.max_column+1):
        s = ws.cell(r,c).value
        if s != None and re.search(' $',s,flags=re.I): 
                ws.cell(r,c).value = re.sub(' $','',s,flags=re.I)
                
for r in range(1,ws.max_row+1):
    for c in range(1,ws.max_column+1):
        s = ws.cell(r,c).value
        if s != None and re.search('^ ',s,flags=re.I): 
                ws.cell(r,c).value = re.sub('^ ','',s,flags=re.I)

print("Text replacement part 4 of 4")
for r in range(1,ws.max_row+1):
    for c in range(1,ws.max_column+1):
        s = ws.cell(r,c).value
        if s != None and re.search('^D ',s,flags=re.I): 
                ws.cell(r,c).value = re.sub('^D ','',s,flags=re.I)
                
for r in range(1,ws.max_row+1):
    for c in range(1,ws.max_column+1):
        s = ws.cell(r,c).value
        if s != None and re.search(' \(L\)$',s,flags=re.I): 
                ws.cell(r,c).value = re.sub(' \(L\)$','',s,flags=re.I)
                ws.cell(r,3).value = ws.cell(r,c).value
                ws.cell(r,c).value = ''
                
ws['C1'].value = 'Not Employed'                
ws['C1'].font = Font(bold=True)   
                
# Saving another file      
wb.save('Imports.xlsx')

writer2=pd.ExcelWriter('Modified.xlsx')

fs1 = pd.read_excel('Imports.xlsx', sheet_name='Sheet1', usecols="A")
fs1 = fs1.sort_values(['ALL_NAME'],ascending=[True])

fs1.to_excel(writer2,index=False,startcol=0)

writer2.save()
count = 0
count2 = 0




#read input file
fin = open("users.txt", "rt")
#read file contents to string
data = fin.read()
#replace all occurrences of the required string
data = data.replace('  Full name: ', '')
data = data.replace('  Account disabled: ', '')
data = data.replace('  User login: ','')
#close the input file
fin.close()
#open the input file in write mode
fin = open("users.txt", "wt")
#overrite the input file with the resulting data
fin.write(data)
#close the file
fin.close()


wb1 = openpyxl.load_workbook("Modified.xlsx")
ws1 = wb1["Sheet1"]

test = 0




searchfile = open("users.txt", "r") 
    #print(ws1.cell(r,1).value)
   

for i, line in enumerate(searchfile):
    global match
    match = 0
    global a 
    a = i
    l = searchfile.readline(i)
    # finding first search boundary 
    if re.search('^  dn: ',l,flags=re.I):
        if match == 0:
            boole = True
            while boole == True:
                l3 = searchfile.readline(a)
                # finding end boundary in the file for data
                if re.search('  objectclass: ',l3,flags=re.I):
                    boole = False
                else:
                    a+=1
    

基本上下面的代碼是問題出現的地方。 我希望代碼迭代整個 excel 文件,並將每個單元格與文本文件中定義的邊界內的每一行進行比較。 然而,while 循環內的值r被重置為 2,而在它進入 while 循環之前,它似乎工作得很好。

        for r in range(2,ws1.max_row+1):
            #print(r)
            while i < a:
                #print(r)
                l2 = searchfile.readline(i)
            
                if re.search("soa$",l2,flags=re.I) and match == 0: 
                    match = 1
                elif re.search('TC$',l2,flags=re.I) and match == 0:
                    match = 1
                elif re.search(l2,ws1.cell(r,1).value,flags=re.I) and match == 0:
                    ws1.cell(r,2).value = "MATCH"
                    match = 1
                
                i+=1
                
            if match == 0:
                ws1.cell(r,4).value = "NO MATCH"

                    
searchfile.close()
wb1.save("Modified.xlsx")

在 while 循環中打印r時,它包含值 2 並且在 while 循環之外,但仍在 for 循環中它按預期工作。 我不知道為什么這個值沒有在 while 循環中被翻譯。 有任何想法嗎?

我認為無論你得到多少 2 都是你的 while 循環經歷的循環數。 我看不到“a”被初始化為什么,但我確實看到您從未在 for 循環中重置“i”。 它進入 for 循環,r =2 一次,它進入 while 循環 r=2 但是它循環了很多次,然后返回到 for 循環,其中 r 現在 =3,並且再也不會進入 while 循環,因為我現在是永久大於 a。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM