将结果从txt提取到csv

时间:2017-06-05 13:40:02

标签: c csv

我试图从文本中提取一些我已经进入csv文件的结果。 results.txt有form,我希望以csv的形式提取它:

Benchmark,        Pass/Fail,     ops/m
compiler.compiler, PASSED,       18.37
compress,          PASSED,       10.87
crypto.aes,        PASSED,        3.91

等...

所以我想以这种形式只保留迭代1结果。你建议我做什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

以下Python 2.x脚本应该有助于您入门(最初标记为)。可以为包含results.txt的行传递iteration 1文件,如下所示:

import csv
from itertools import ifilter

with open('results.txt', 'rb') as f_input, open('output.csv', 'wb') as f_output:
    csv_input = csv.reader(ifilter(lambda x: "iteration 1" in x, f_input), delimiter=' ', skipinitialspace=True)
    csv_output = csv.writer(f_output)
    csv_output.writerow(["Benchmark", "Pass/Fail", "ops/m"])

    for row in csv_input:
        csv_output.writerow([row[0], 'PASSED', row[6]])

按如下方式创建输出CSV文件:

Benchmark,Pass/Fail,ops/m
compiler.compiler,PASSED,18.37
compress,PASSED,10.87
crypto.aes,PASSED,3.91
crypto.rsa,PASSED,8.79
crypto.signverify,PASSED,15.10
derby,PASSED,9.40
mpegaudio,PASSED,7.81
scimark.fft.large,PASSED,4.27
scimark.lu.large,PASSED,0.85
scimark.sor.large,PASSED,2.38
scimark.sparse.large,PASSED,1.46
scimark.monte_carlo,PASSED,5.65
scimark.fft.small,PASSED,8.94
scimark.lu.small,PASSED,8.24
scimark.sor.small,PASSED,12.90
scimark.sparse.small,PASSED,5.61
serial,PASSED,4.53
startup.helloworld,PASSED,41.24
startup.compiler.compiler,PASSED,2.05
startup.compress,PASSED,3.62
startup.crypto.aes,PASSED,0.92
startup.crypto.rsa,PASSED,1.87
startup.crypto.signverify,PASSED,2.76
startup.mpegaudio,PASSED,1.82
startup.scimark.fft,PASSED,4.49
startup.scimark.lu,PASSED,2.44
startup.scimark.monte_carlo,PASSED,1.27
startup.scimark.sor,PASSED,3.14
startup.scimark.sparse,PASSED,1.54
startup.serial,PASSED,1.73
startup.sunflow,PASSED,3.55
startup.xml.transform,PASSED,0.27
startup.xml.validation,PASSED,4.28
sunflow,PASSED,3.69
xml.transform,PASSED,10.41
xml.validation,PASSED,15.37

但是,它只是假设文件中的所有条目都被视为PASSED,因为没有示例显示示例文件中的失败条目。