我有一个大日志文件,其中包含信息,我感兴趣的唯一信息如下所示
.....
.....
.....
I0813 11:58:19.631247 30700 solver.cpp:189] Iteration 500, loss = 0.0922444
I0813 11:58:19.631288 30700 solver.cpp:470] Iteration 500, lr = 0.001
.....
.....
.....
I0813 12:05:11.543995 30700 solver.cpp:189] Iteration 520, loss = 0.0299977
I0813 12:05:11.544034 30700 solver.cpp:470] Iteration 520, lr = 0.001
.....
.....
.....
I0813 12:10:11.543995 30700 solver.cpp:189] Iteration 420, loss = 0.0299977
I0813 12:10:11.544034 30700 solver.cpp:470] Iteration 540, lr = 0.001
如何在python中提取这些行并提取关键数据,如iteration,lr,loss和timestamp,并保存在另一个文件中。
注意,尊贵的数据就像 solver.cpp:189迭代或solver.cpp:470迭代
答案 0 :(得分:2)
logfile = r'C:\logfile.txt'
y = open(r'C:\extract.txt', 'a')
with open(logfile, 'r') as read:
for line in read:
if "solver.cpp:189" in line:
loss = line.split(',')[1]
y.write(loss)
if "solver.cpp:470" in line:
lr = line.split(',')[1]
y.write(lr)
同样,你可以分开' ='得到这个数字。或者如果你想更具体,你可以分开' lr ='或者'损失='。这实际上取决于您打算如何应用它以及日志的其余部分。