Extracting multiple strings using Pythons's regular expression

后端 未结 3 489
北荒
北荒 2021-01-27 21:42

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         


        
3条回答
  •  温柔的废话
    2021-01-27 22:27

    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))
    ...
    

提交回复
热议问题