I0625 17:25:22.544378 3366 solver.cpp:229] Iteration 7120, loss = 8.79839
预期输出:
Iteration 7120 loss = 8.79839
答案 0 :(得分:0)
您可能需要使用正则表达式来查找包含所需值的行并提取它们。然后可以使用Python的CSV库以CSV格式编写这些行,如下所示:
import re
import csv
with open('log.txt') as f_input, open('output.csv', 'w', newline='') as f_output:
csv_output = csv.writer(f_output)
csv_output.writerow(['Iteration', 'loss'])
for line in f_input:
re_values = re.search(r'Iteration (\d+), loss = ([0-9.]+)', line)
if re_values:
csv_output.writerow(re_values.groups())
以CSV格式给您output.csv
,如下所示:
Iteration,loss
7120,1.79839
7120,1.79839
7120,1.79839
7120,1.79839