比较文本文件内容

2024-04-16 08:18:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个小脚本来比较一个文本文件的内容与另一个包含单词列表的文本文件,但是运行它时说找不到匹配项,我无法修复代码以成功地将它们与正确的结果进行比较。你知道吗

wordlist = input("What is your word list called?")
f = open(wordlist)
t = f.readlines()
l = ''.join(t).lower()
chatlog = input("What is your chat log called?")
with open(chatlog) as f:
   found = False
   for line in f:
       line = line.lower()
       if l in line:
          print(line)
          found = True
   if not found:
      print("not here")

Tags: ininputyourifislineopenlower
1条回答
网友
1楼 · 发布于 2024-04-16 08:18:42
wordlist = input("What is your word list called?")
f = open(wordlist)
l = set(w.strip().lower() for w in f)
chatlog = input("What is your chat log called?")
with open(chatlog) as f:
   found = False
   for line in f:
       line = line.lower()
       if any(w in line for w in l):
          print(line)
          found = True
   if not found:
      print("not here")

相关问题 更多 >