Я пишу код для извлечения чего-то полезного из очень большого 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
, чтобы он также был отмечен в моем файле Output.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
оттуда.Ценю вашу помощь.