I am using python 3.8 for an application where problem is such that: I have two files
one is actual_results.txt file that consists of one string each line that is for example line 1 "Encryption Has Failed" Line 2 " Random Numbers generator passed" . . . Line N etc Then the second file is expected_results that will have multiple strings in each line which will represent possible outcomes of each test results e.g Line 1 "Encryption Has Failed, Encryption Passed, Data Corruption, False Key Used ..." Similarly, Line 2 "Random Numbers generator has failed, Random Numbers generator Passed, Data Corruption, ..." . . . Line N
Based on two files comparison between each lines a third file will be generated which will represent results on each line as: found or not found from expected results. What I have tried so far is as follows:
with open('actual_results.txt', 'r') as file1:
with open('expected_results.txt', 'r') as file2:
same = set(file1).intersection(file2)
with open('final_output_file.txt', 'w') as file_out:
for line in same:
file_out.write(line)
This gives output based on full line comparison rather than comparing multiple strings in each line of one file(expected_results) to single string in each line of another file(actual_result.txt) .
Question is How can I compare multiple comma separated strings in one file's lines to same line of the other file with one string?
I have thought about a solution which i am not sure would work.
iterate through each line of one file with multiple strings to separate using
line.split(',')
to put into a list while comparing that list each item with single string on same line number on actual_results file.
You are comparing every line of the first file to every line of the second file. What I think you want to do is compare each file line by line.
Something like this?
lines = []
with open('actual_results.txt') as actual, open('expected_results.txt') as expected:
try:
while True:
a, e = next(actual), next(expected)
if a in e.split(','):
lines.append((a, e, True))
else:
lines.append((a, e, False))
except StopIteration:
pass
with open('final_output_file.txt', 'w') as output:
for actual, expected, result in lines:
if result:
output.write('found\n')
else:
output.write('not found\n')