Я пытаюсь получить значения из файла журнала, используя регулярное выражение Python, и в этом процессе у меня есть несколько операторов if
. Раздел кода, который захватывает значения следующим образом:Python multiple if statements
# Opening the log file for reading
with open(logFile, 'r') as logfile_read:
for line in logfile_read:
line = line.rstrip()
# To extract Time or iteration
if 'Time' in line:
iteration_time = re.findall(r'^Time\s+=\s+(.*)', line)
# To extract local, global and cumulative values
if 'local' in line:
local_global_cumu = re.search(r'sum\s+local\s+=\s+(.*),\s+global\s+=\s+(.*),\s+cumulative\s+=\s+(.*)', line)
if local_global_cumu:
contLocal_0_value = local_global_cumu.group(1)
contGlobal_0_value = local_global_cumu.group(2)
contCumulative_0_value = local_global_cumu.group(3)
for t in iteration_time:
contLocal.write("%s\t%s\n" %(t, contLocal_0_value))
contGlobal.write("%s\t%s\n" %(t, contGlobal_0_value))
contCumulative.write("%s\t%s\n" %(t, contCumulative_0_value))
# To extract execution and cpu time
if 'ExecutionTime' in line:
execution_cpu_time = re.search(r'^ExecutionTime\s+=\s+(.*)\s+s\s+ClockTime\s+=\s+(.*)\s+s', line)
if execution_cpu_time:
execution_time_0_value = execution_cpu_time.group(1)
cpu_time_0_value = execution_cpu_time.group(2)
for t in iteration_time:
print t
Во втором if
заявление, я могу получить значения из t
. Однако в последующем заявлении if
, когда я пытаюсь сделать print t
, ничего не приходит. Я не знаю, где я ошибся.
Если это не петля. [И вы используете, если неправильно.] (Http://stackoverflow.com/questions/20002503/why-does-ab-or-c-or-d-always-evaluate-to-true) –
Я бы предположил что 'iteration_time' пуст, или тело' if ('ExecutionTime' или 'ClockTime') в строке: 'никогда не выполняется. –
Спасибо @Ashwini Chaudhary. Можете ли вы дать мне несколько намеков? – hypersonics