我正在尝试创建一个脚本,该脚本将
我想出了将文本存储在文件中的以下内容,但是我似乎无法获得比较列表和打印相似性的功能。
KeyWords = open("wordlist.txt").readlines() # Opens WordList text file, Reads each line and stores in a list named "KeyWords".
RanText = open("RandomText.txt").readlines() # Opens RandomText text file, reads each line and stores in a list named "RanText"
def Check():
for x in KeyWords:
if x in RanText:
print(x)
print(KeyWords)
print(RanText)
print(Check)
输出:
C:\Scripts>python Search.py
['Word1\n', 'Word2\n', 'Word3\n', 'Word4\n', 'Word5']
['Lorem ipsum dolor sit amet, Word1 consectetur adipiscing elit. Nunc fringilla arcu congue metus aliquam mollis.\n', 'Mauris nec maximus purus. Maecenas sit amet pretium tellus. Praesent Word3 sed rhoncus eo. Duis id commodo orci.\n', 'Quisque at dignissim lacus.']
<function Check at 0x00A9B618>
答案 0 :(得分:1)
print(Check)
仅打印对象,仅调用函数
check()
此函数内的print语句将起作用并打印您编写的内容。 出于测试目的,只需创建两个列表,如果比较功能可以工作,则尝试从文件列表中进行尝试。
或仅使用compare two list
中的解决方案答案 1 :(得分:1)
您应该调用print(Check)
而不是Check()
,因为它会打印并且不会返回任何内容。
另一个选择是:
KeyWords = []
with open("wordlist.txt") as f:
for line in f:
KeyWords.append(line.strip())
RanText = []
with open("RandomText.txt") as f:
for line in f:
RanText.append(line.strip())
in_common = [word for word in KeyWords if word in RanText]
print(in_common)