Python:分配给变量不起作用

时间:2020-08-19 13:59:13

标签: python

我是python的新手,但不是编程人员。我的问题可能与缺乏语法知识有关。

我正在尝试从日志文件中检索部分日志。当我开始读取日志文件时,日志文件可能仍在生成(可能有一个开始但没有结尾),因此我尝试循环等待结尾。该脚本在无限循环中运行,当我尝试对其进行调试时,已经达到了一个分配,但未分配该分配,并且使退出条件始终为假。

try:
endMatches = ['***** Fin normale *****', '***** Fin anormale *****']
startMatches = ['*** START OF ']
endAttributes = []
startAttributes = []
stopSearch = False

while not stopSearch:
    if not Path(pathToLog).exists():
        continue
    logFile = open(pathToLog)
    jobLog = logFile.readlines()
    reversedLog = []
    lastloggedLine=[]
    
    if len(reversedLog) < len(jobLog):
        
        reversedLog = reversed(list(enumerate(jobLog)))
        for i, e in reversedLog:
            if any(x in e for x in endMatches):
                if endAttributes != [] and (endAttributes[2] < datetime.strptime(e[:15], '%Y%m%d %H%M%S')):
                    endAttributes = [i, e, datetime.strptime(e[:15], '%Y%m%d %H%M%S')]
                elif endAttributes == []:   
                    endAttributes = [i, e, datetime.strptime(e[:15], '%Y%m%d %H%M%S')]
            if any(x in e for x in startMatches):
                if startAttributes != [] and (startAttributes[2] < datetime.strptime(e[:15], '%Y%m%d %H%M%S')):
                    startAttributes = [lastloggedLine[0], lastloggedLine[1], datetime.strptime(lastloggedLine[1][:15], '%Y%m%d %H%M%S')]
                elif startAttributes == []:
                    startAttributes = [lastloggedLine[0], lastloggedLine[1], datetime.strptime(lastloggedLine[1][:15], '%Y%m%d %H%M%S')]
            if startAttributes != [] and endAttributes != [] and (launchJob == False or (launchJob == True and startAttributes[2] > jobLaunchTime)) and startAttributes[2] < endAttributes[2]:
                stopSearch = True
                break
            lastloggedLine = [i, e]

因此,我尝试在文件中查找最后的开始和结束行,如果同时找到了它们,则是文件中的最后一条,开始行位于结束行之前,并且日志的第一行包含执行在脚本启动之后发生的时间,stopsearch必须让我离开。

在调试时,我注意到脚本正确找到了结束行并将其分配给endAttributes。起始行也正确找到,该行

 startAttributes = [lastloggedLine[0], lastloggedLine[1], datetime.strptime(lastloggedLine[1][:15], '%Y%m%d %H%M%S')]

已到达,但之后startAttributes仍为空。

这是分配给startAttributes之后的调试屏幕截图:

enter image description here

0 个答案:

没有答案
相关问题