需要匹配文本文件中的一组单词。 这组单词分别以不同的行显示。 许多事件并不重要,但必须至少发生一次。 只有当所有单词完全匹配时才是通过,否则测试是失败。
我创建了一个包含内容的文件:
file1 = open("MyFile.txt","a+")
现在,列表是
list = ["SIMPLE", "QUICK", "ADVANCED"]
下面的代码适用于单个字符串,但不适用于列表。
with open("C:/Users/vikp/Desktop/MyFile.txt") as file1:
for line in file1:
if list in line:
<assert pass condition>
else:
<assert fail condition>
答案 0 :(得分:1)
每行只有一个字,对吗?如果是这种情况,您可以通过line in mylist
测试该行的有效性。该代码假定您不允许除mylist
之外的任何单词。
occurred = set() # this set tests for at least one occurrence of each word
with open("C:/Users/vikp/Desktop/MyFile.txt") as file1:
for word in file1:
word = word.strip() # get rid of new-line or whitespace characters
if word in mylist: # assume one word per line
occurred.add(word)
else:
raise ValueError(word + ' is not in mylist')
# success if the for loop finishes without error AND all words occurred at least once
if len(occurred) == len(mylist):
print('success')
else:
missing = set(mylist) - occurred
raise ValueError('the following words are missing: '+str(missing))
答案 1 :(得分:0)
在那里使用set而不是list,您可以使用regex尝试此解决方案:
import re
list = {"SIMPLE", "QUICK", "ADVANCED"}
with open('file.txt','r') as f:
for line in f:
match=set()
for item in list:
if re.findall(item,line):
match.add("".join(re.findall(item,line)))
if list-match==set():
print(" Test pass")
#<assert pass condition>
else:
print("Test fail")
#<assert fail condition>
我使用file.txt进行测试,其中包含:
SIMPLE and QUICK are winners they are ADVANCED too.
SIMPLE and QUICK are winners they are ADVANCED too.
SIMPLE and QUICK are winners they are ADVANCED too wowow.
输出:
Test pass
Test pass
Test pass
如果您的单词不是我尝试使用虚拟文件,那么正则表达式将完全匹配。