简体   繁体   English

如何找到(如果存在)txt 文件中搜索到的字符串同时匹配的那一行?

[英]How to find (if it exists) that line of a txt file where the searched strings match at the same time?

I need to verify if there is a line where these 2 matches are met in the respective.txt:我需要验证在 respective.txt 中是否有一行满足这两个匹配项:

  • Line of the file named file1.txt that is equal to the input string1 "old apple" .名为file1.txt的文件的行等于输入 string1 "old apple"
  • Line of the file named file2.txt that is equal to the input string2 "on the floor" .名为file2.txt的文件的行等于输入 string2 "on the floor"

For example, file1.txt have these lines:例如, file1.txt有这些行:

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

And for example, file2.txt have these other lines:例如, file2.txt有这些其他行:

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在这种情况下,正确的行是第 4 行,即索引为 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?我该如何处理这种双重匹配算法?

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.但即使您会检查,由于 .index 的工作方式,您的代码也可能会产生错误的.index 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 .返回列表中第一项的值等于x的从零开始的索引。 Raises a ValueError if there is no such item.如果没有这样的项目,则引发ValueError

... ...

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
    ]

if matches:
    print("Mutual coincidence condition is satisfied with these inputs!")
    print(f"Rows with matches: {', '.join(str(i) for i in matches)}")
else:
    print("Mutual coincidence condition is not satisfied with these inputs!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM