如何找到(如果存在)txt 文件中搜索到的字符串同时匹配的那一行?
How to find (if it exists) that line of a txt file where the searched strings match at the same time?
我需要验证在相应的 .txt 中是否有一行满足这两个匹配项:
- 名为
file1.txt
的文件的行等于输入字符串 1 "old apple"
。
- 名为
file2.txt
的文件的行等于输入字符串 2 "on the floor"
。
例如,file1.txt
有这些行:
juicy orange
old apple
rusty key
old apple
modern table
old apple
例如,file2.txt
还有这些其他行:
in a box
on the chair
on the bed
on the floor
on the floor
under the desktop
在这种情况下,正确的行是第 4 行,即索引为 3 的行
这是我的代码,但我遇到了相互匹配要求的问题
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!")
如果其中一个输入匹配但位于文件的不同行,则此代码失败。
我该如何处理这种双重匹配算法?
在您的代码中,您只检查相应列表中是否存在字符串,而不检查相应的行号是否匹配。但即使您会检查,由于 .index
的工作方式,您的代码也可能会产生错误的输出。引用自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.
...
你只得到第一个索引。因此,如果没有与各自的第一个发现相匹配的行号,您就会被卡住。
如果您只对是否有匹配项感兴趣,那么您可以这样做:
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!")
如果您也对提供匹配的行号感兴趣:
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!")
我需要验证在相应的 .txt 中是否有一行满足这两个匹配项:
- 名为
file1.txt
的文件的行等于输入字符串 1"old apple"
。 - 名为
file2.txt
的文件的行等于输入字符串 2"on the floor"
。
例如,file1.txt
有这些行:
juicy orange
old apple
rusty key
old apple
modern table
old apple
例如,file2.txt
还有这些其他行:
in a box
on the chair
on the bed
on the floor
on the floor
under the desktop
在这种情况下,正确的行是第 4 行,即索引为 3 的行
这是我的代码,但我遇到了相互匹配要求的问题
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!")
如果其中一个输入匹配但位于文件的不同行,则此代码失败。
我该如何处理这种双重匹配算法?
在您的代码中,您只检查相应列表中是否存在字符串,而不检查相应的行号是否匹配。但即使您会检查,由于 .index
的工作方式,您的代码也可能会产生错误的输出。引用自here:
list.index(x[, start[, end]])
Return zero-based index in the list of the first item whose value is equal to
x
. Raises aValueError
if there is no such item....
你只得到第一个索引。因此,如果没有与各自的第一个发现相匹配的行号,您就会被卡住。
如果您只对是否有匹配项感兴趣,那么您可以这样做:
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!")
如果您也对提供匹配的行号感兴趣:
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!")