0

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.

1
  • Hi, it would help if you shared a sample of those files along with the expected result. Commented Feb 2, 2020 at 11:45

4 Answers 4

1

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')
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to check if a string is one in a list, use an expression like this:

search_string in (list_of_candidates)

Comments

0

You could also try:

with open('actual_results.txt', 'r') as file1:
    actual = file1.readlines()

with open('expected_results.txt', 'r') as file2:
    expected = file2.readlines()

with open('final_output_file.txt', 'w') as file_out:
    for a, e in zip(actual, expected):
        file_out.write('found\n' if a.strip() in e.strip().split(', ') else 'not found\n')

Comments

0

You maybe don't want to but you could use pandas to do the heavy lifting.

import pandas as pd

actuals = pd.read_csv('./actuals.csv', header=None)
expected = pd.read_csv('./expected.csv', header=None, delimiter='\n')

df = pd.concat([actuals, expected], axis=1, ignore_index=True)

df.apply(lambda row: True if row[0] in row[1].split(',') else False, axis=1)

Where actuals dataframe is:

        0
0  error1
1  error2
2  error3

The expected dataframe is:

                      0
0  error1,error4,error5
1  error2,error6,error7
2         error8,error9

The concatenated dataframe is:

        0                     1
0  error1  error1,error4,error5
1  error2  error2,error6,error7
2  error3         error8,error9

And the comparison yields:

0     True
1     True
2    False

My actuals.csv file contained:

error1
error2
error3

And my expected.csv file contained:

error1,error4,error5
error2,error6,error7
error8,error9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.