我正在尝试迭代测试结果文件(test.csv),并寻找包含从文本文件(index.txt)中读取的关键字的特定行。
下面是我的代码,但它不起作用。
indexTxt = open('index.txt', 'r')
indexLines = indexTxt.readlines()
results = open('test.csv', 'r')
resultLines = results.readlines()
for indexLine in indexLines:
for line in resultsLines:
if indexLine in line:
print ("Read Line1: %s" % line)
假设我的index.txt包含以下内容:
TestLine1
TestLine4
TestLine6
我喜欢搜索并打印出匹配的行。
感谢你的建议。
感谢
问题是if indexLine in line
。如果它们完全相同,if indexLine == line
可能会起作用,或者您可能想使用.strip()
来删除额外的空白。
我还建议在循环中添加一个print("index: '%s', text: '%s'", indexLine, line)
或类似的语句来仔细检查精确的文本。