I have a log file having the following output and I have shortened it as it goes to thousands of lines:
Time = 1
smoothSolver: Solving for Ux, Initial residual
When there is no 'Time' or 'cumulative' in this line, there is no need to overwrite that variable. You can do this:
...
with open(logFile, 'r') as logfile_read:
for line in logfile_read:
line = line.rstrip()
if 'Time' in line:
iteration_time = re.findall(r'^Time = ([0-9]+)', line)
print iteration_time
if 'cumulative' in line:
contCumulative_0 = re.search(r'cumulative = ((\d|.)+)', line)
if contCumulative_0:
cumvalue = contCumulative_0.groups(1)
contCumulative_0_out.write('\n'.join(cumvalue))
...