我正在编写一个代码,从一个非常大的Source.txt
文件中提取一些有用的东西。
我的源测试文件示例如下:
Test case AAA
Current Parameters:
Some unique param : 1
Some unique param : 2
Some unique param : 3
Some unique param : 4
*A line of rubbish*
*Another line of rubbish*
*Yet another line of rubbish*
*More and more rubbish*
Test AAA PASS
Test case BBB
Current Parameters:
Some unique param : A
Some unique param : B
Some unique param : C
Some unique param : D
*A line of rubbish*
*Another line of rubbish*
*Yet another line of rubbish*
*More and more rubbish*
Test BBB PASS
现在我正在编写一个代码,只提取Test case
和Current Parameters
:
processed = []
def main():
source_file = open("Source.txt","r") #Open the raw trace file in read mode
if source_file.mode == "r":
contents = source_file.readlines() #Read the contents of the file
processed_contents = _process_content(contents)
output_file = open("Output.txt","w")
output_file.writelines(processed_contents)
pass
def _process_content(contents):
for raw_lines in contents:
if "Test case" in raw_lines:
processed.append(raw_lines)
elif "Current Parameters" in raw_lines:
processed.append(raw_lines)
#I am stuck here
elif "PASS" in raw_lines or "FAIL" in raw_lines:
processed.append(raw_lines)
processed.append("\n")
return processed
#def _process_parameters():
if __name__ == '__main__':
main()
在第Current Parameters
行之后,我想抓取每个Some unique param
行,它们将不总是相同的,并附加到processed
列表中,以便在我的输出.txt你知道吗
我想要的输出是:
Test case AAA
Current Parameters:
Some unique param : 1
Some unique param : 2
Some unique param : 3
Some unique param : 4
Test AAA PASS
Test case BBB
Current Parameters:
Some unique param : A
Some unique param : B
Some unique param : C
Some unique param : D
Test BBB PASS
如果你看到了,我想把所有的垃圾线都移走。请注意,在我的Source.txt
中有很多垃圾。我不知道如何从那里去下一个raw_lines
。谢谢你的帮助。你知道吗
可以使用regex back引用(例如
\2
)拆分测试用例(regex101):印刷品:
很难说这是否有效,因为我不知道垃圾行的格式,但我认为您可以检查该行是否包含
"Param"
,就像您为其他行所做的一样:这是一种使用Regex的方法。你知道吗
例如:
输出:
相关问题 更多 >
编程相关推荐