且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Python 的正则表达式提取多个字符串

更新时间:2022-05-20 22:38:20

当该行中没有 'Time' 或 'cumulative' 时,无需覆盖该变量.你可以这样做:

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