[英]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.