如何从文本文件中提取特定数据,以及如何将文本及其旁边显示的值导出到excel。现在,我可以导出所需的数据列表,但是不确定如何获取文本旁边显示的值。文本文件具有以下信息:
Total Preprocessing Time: 231.225
Total Find Global Min Max Time: 151.913
Total Setup Geometry Time: 0.016
Total Other time: 15.180
Total Time: 720.881
我想捕获文本旁边的时间并导出为excel。 我已经使用了该网站中提供的一些示例来提取我想要的特定数据,并且它确实有效。
infile = r"D:\GPU\App1.log"
important = []
keep_phrases = ["Total Preprocessing Time:",
"Total Find Global Min Max Time:"
"Total Setup Geometry Time:",
"Total Other time:",
"Total Time:"]
with open(infile) as f:
for line in f:
for phrase in keep_phrases:
if phrase in line:
important.append(line)
break
print(important)
infile = open('output.csv', 'w')
for row in keep_phrases:
for column in row:
infile.write("%s" % column)
infile.write('\n')
infile.close()
这是它在excel中导出文本的方式。
Total Preprocessing Time:
Total Find Global Min Max Time:
Total Setup Geometry Time:
Total Other time:
Total Time: