在Python中读取日志文件

时间:2018-07-05 05:43:10

标签: python python-2.7

我有一个包含以下内容的日志文件。 我喜欢读取Iteration valuedetection_eval的值

I0704 18:10:31.097334  2421 solver.cpp:433] Iteration 200, Testing net (#0)
I0704 18:10:31.149454  2421 net.cpp:693] Ignoring source layer mbox_loss
I0704 18:10:40.241916  2421 solver.cpp:546]     Test net output #0: detection_eval = 0.00273318

我做了

accuracy_pattern = r"Iteration (?P<iter_num>\d+), Testing net \(#0\)\n.* detection_eval = (?P<accuracy>[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?)"

但是什么都没读,这是什么问题?

编辑: 然后,我读取了precision_pattern以数组形式

for r in re.findall(accuracy_pattern, log):
        iteration = int(r[0])
        accuracy = float(r[1]) * 100

日志包含所有文件内容,其内容如下

with open(log_file, 'r') as log_file2:
        log = log_file2.read()

2 个答案:

答案 0 :(得分:2)

据我了解您的数据,以下正则表达式应该有效:

pattern = "Iteration\s+(\d+)|detection_eval\s+=\s+(.+$)"
for it,de in re.findall(pattern, log, flags=re.M):
    if it: 
        print('Iteration', int(it))
    if de:
        print('detection_eval', float(de))
#Iteration 200
#detection_eval 0.00273318

但是,一次读取整个日志文件通常不是一个好主意。考虑一次读取一行:

with open(log_file, 'r') as log_file2:
    for line in log_file2:
        for it,de in re.findall(pattern, log):
            if it: 
                print('Iteration', int(it))
            if de:
                print('detection_eval', float(de))

答案 1 :(得分:0)

使用re.search

演示:

import re

with open(log_file, "r") as infile:
    for line in infile:
        iteration = re.search("Iteration (\d+)", line)
        if iteration:
            print iteration.group()

        detection_eval = re.search("detection_eval = (\d.*)", line)
        if detection_eval:
            print detection_eval.group()

输出:

Iteration 200
detection_eval = 0.00273318

或使用re.findall

iteration = re.findall(r"Iteration (\d+)", log )
detection_eval = re.findall(r"detection_eval = (\d.*)", log )