Home > Back-end >  How to find (if it exists) that line of a txt file where the searched strings match at the same time
How to find (if it exists) that line of a txt file where the searched strings match at the same time

Time:02-28

I need to verify if there is a line where these 2 matches are met in the respective .txt :

  • Line of the file named file1.txt that is equal to the input string1 "old apple".
  • Line of the file named file2.txt that is equal to the input string2 "on the floor".

For example, file1.txt have these lines:

juicy orange
old apple
rusty key
old apple
modern table
old apple

And for example, file2.txt have these other lines:

in a box
on the chair
on the bed
on the floor
on the floor
under the desktop

In this case the correct line would be line 4, that is, the one with index 3

This is my code but I am having problems with the mutual matching requirement

string1 = "old apple"
string2 = "on the floor"

num_linea_data_field_1 = None
num_linea_data_field_2 = None

validation_data_field_1 = False
validation_data_field_2 = False

with open("file1.txt","r ") as f:
    lineas = [linea.strip() for linea in f.readlines()]
    if (string1 not in lineas):
        pass
    else:
        num_linea_data_field_1 = lineas.index(string1)
        validation_data_field_1 = True

with open("file2.txt","r ") as f:
    lineas = [linea.strip() for linea in f.readlines()]
    if (string1 not in lineas):
        pass
    else:
        num_linea_data_field_2 = lineas.index(string2)
        validation_data_field_2 = True


if(validation_data_field_1 == True and validation_data_field_2 = True):
    print("Mutual coincidence condition is satisfied with these inputs!")

else:
    print("Mutual coincidence condition is not satisfied with these inputs!")

This code is failing in cases where one of the inputs matches but on different lines of the file.

How could I approach this double matching algorithm?

CodePudding user response:

In your code you only check if the strings are present in the respective lists, you don't check if the corresponding line numbers match. But even if you would check, your code is likely to produce an erroneous output because of how .index works. To quote from here:

list.index(x[, start[, end]])

Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item.

...

You only get the first index. So, if there's no line number match for the respective first finds, you'd be stuck.

If you're only interested in if there's a match then you could do something like this instead:

with open("file1.txt", "r") as file1,\
     open("file2.txt", "r") as file2:
    matches = any(
        row1.strip() == string1 and row2.strip() == string2
        for row1, row2 in zip(file1, file2)
    )

if matches:
    print("Mutual coincidence condition is satisfied with these inputs!")
else:
    print("Mutual coincidence condition is not satisfied with these inputs!")

If you are also interested in the line numbers that provide a match:

with open("file1.txt", "r") as file1,\
     open("file2.txt", "r") as file2:
    matches = [
        i for i, (row1, row2) in enumerate(zip(file1, file2), start=1)
        if row1.strip() == string1 and row2.strip() == string2
    ]
...
  • Related