Python嵌套循环 - 继续迭代第一个循环

时间:2015-08-17 21:35:05

标签: python csv python-3.x nested-loops

全新的编程,但非常愉快的挑战。 这是一个我怀疑可能是由于对python循环的误解造成的问题。 系统信息:在Win 7 32位上使用notepad ++和IDLE python 3.4.3

我的解决方案是打开1个数据库,使用它从数据库2中查找正确的主条目,提取索引号(task_no),然后写入与第一个数据库相同的第3个文件,这次使用正确的索引号。 我的问题是它正确地执行第一和第二循环,然后在循环1的第二次迭代中,尝试在循环2中执行一个块,同时迭代循环1的行,而不是循环2的task_rows。

脚注:两个文件都很大(几MB),所以我要注意将它们存储在内存中是个好主意。

这是我发现最接近这个问题的相关问题: python nested loop using loops and files

我得到的是我尝试在第一个循环内移动文件打开,但问题仍然存在。与我如何使用CSV阅读器有关?

我也怀疑可能存在解决问题的根本原因,所以我欢迎提出解决问题的替代方法的建议。 提前谢谢!

要点:

for row in readerCurrentFile: #LOOP 1
    # iterates through readerCurrentFile to define search variables
    [...]
    for task_row in readerTaskHeader: #LOOP 2
        # searches each row iteratively through readerTaskHeader
        # Match compid
        #if no match, continue <<<- This is where it goes back to 1st loop
        [...]
        # Match task frequency
        #if no match, continue
        [...]
        # once both of the above matches check out, will grab data (task_no from task_row[0]
        task_no = ""
        task_no = task_row[0]
        if task_row:
            break
   [...]
    # writes PM code
    print("Successful write of PM schedule row")
    print(compid + " " + dict_freq_names[str(pmfreqx) + str(pmfreq)] + ": " + pmid + " " + task_no)

整个代码:

import csv
import re
#Writes schedule
csvNewPMSchedule = open('new_pm_schedule.csv', 'a', newline='')
writerNewPMSchedule = csv.writer(csvNewPMSchedule)

# Dictionaries of PM Frequency
def re_compile_dict(d,f):
    for k in d:
        d[k] = re.compile(d[k], flags=f)

dict_month = {60:'Quin',36:'Trien',24:'Bi-An',12:'Annual(?<!Bi-)(?<!Semi-)',6:'Semi-An',3:'Quart',2:'Bi-Month',1:'Month(?<!Bi-)'}
dict_week = {2:'Bi-Week',1:'Week(?<!Bi-)'}
dict_freq_names = {'60Months':'Quintennial','36Months':'Triennial','24Months':'Bi-Annual','12Months':'Annual','6Months':'Semi-Annual','3Months':'Quarterly','2Months':'Bi-Monthly','1Months':'Monthly','2Weeks':'Bi-Weekly','1Weeks':'Weekly'}

re_compile_dict(dict_month,re.IGNORECASE)
re_compile_dict(dict_week, re.IGNORECASE)

# Unique Task Counter
task_num = 0
total_lines = 0

#Error catcher
error_in_row = []

#Blank out all rows
pmid = 0
compid = 0
comp_desc = 0
pmfreqx = 0
pmfreq = 0
pmfreqtype = 0

# PM Schedule Draft (as provided by eMaint)
currentFile = open('pm_schedule.csv', encoding='windows-1252')
readerCurrentFile = csv.reader(currentFile)

# Loop 1
for row in readerCurrentFile:
    if row[0] == "pmid":
        continue
    #defines row items
    pmid = row[0]
    compid = row[1]
    comp_desc = row[2]
    #quantity of pm frequency
    pmfreqx_temp = row[3]
    #unit of pm frequency, choices are: Months, Weeks
    pmfreq = row[4]
    #pmfreqtype is currently only static not sure what other options we have
    pmfreqtype = row[5]
    #pmnextdate is the next scheduled due date from this one. we probably need logic later that closes out any past due date
    pmnextdate = row[6]
    # Task Number This is what we want to change
    # pass
    # We want to change this to task header's task_desc
    sched_task_desc = row[8]
    #last done date
    last_pm_date = row[9]
    #
    #determines frequency search criteria
    #
    try:
        pmfreqx = int(pmfreqx_temp)
    except (TypeError, ValueError):
            print("Invalid PM frequency data, Skipping row " + pmid)
            error_in_row.append(pmid)
            continue
    #
    #defines frequency search variable
    #
    freq_search_var = ""
    if pmfreq == "Weeks":
        freq_search_var = dict_week[pmfreqx]
    elif pmfreq == "Months":
        freq_search_var = dict_month[pmfreqx]
    if not freq_search_var:
        print("Error in assigning frequency" + compid + " " + str(pmfreqx) + " " + pmfreq)
        error_in_row.append(pmid)
        continue
        #defines Equipment ID Search Variable
    print(compid + " frequency found: " + str(pmfreqx) + " " + str(pmfreq))
    compid_search_var = re.compile(compid,re.IGNORECASE)
    #
    # Matching function - search taskHeader for data
    #
    #PM Task Header Reference 
    taskHeader = open('taskheader.csv', encoding='windows-1252')
    readerTaskHeader = csv.reader(taskHeader)
    for task_row in readerTaskHeader:
        # task_row[0]: taskHeader pm number
        # task_row[1]: "taskHeader task_desc
        # task_row[2]: taskHeader_task_notes
        #
        # search for compid
        compid_match = ""
        compid_match = compid_search_var.search(task_row[1])
        if not compid_match:
            print(task_row[1] + " does not match ID for " + compid + ", trying next row.") #debug 2
            continue # <<< STOPS ITERATING RIGHT OVER HERE
        print("Found compid " + task_row[1]) # debug line
        #
        freq_match = ""
        freq_match = freq_search_var.search(task_row[1])
        if not freq_match:
            print(task_row[1] + " does not match freq for " + compid + " " + dict_freq_names[str(pmfreqx) + str(pmfreq)] + ", trying next row.") #debug line
            continue
        print("Frequency Match: " + compid + " " + dict_freq_names[str(pmfreqx) + str(pmfreq)]) # freq debug line
        #
        task_no = ""
        print("Assigning Task Number to " + task_row[0])
        task_no = task_row[0]
        if task_row:
            break
    #
    #error check
    #
    if not task_no:
        print("ERROR IN SEARCH " + compid + " " + pmid)
        error_in_row.append(pmid)
        continue
    #
    # Writes Rows
    #
    writerNewPMSchedule.writerow([pmid,compid,comp_desc,pmfreqx,pmfreq,pmfreqtype,pmnextdate,task_no,sched_task_desc,last_pm_date])
    print("Successful write of PM schedule row")
    print(compid + " " + dict_freq_names[str(pmfreqx) + str(pmfreq)] + ": " + pmid + " " + task_no)
    print("==============")
# Error reporting lined out for now
# for row in error_in_row:
#   writerNewPMSchedule.writerow(["Error in row:",str(error_in_row[row])])
#   print("Error in row: " + str(error_in_row[row]))
print("Finished")

0 个答案:

没有答案