当前位置: 代码迷 >> python >> 比较文本文件内容
  详细解决方案

比较文本文件内容

热度:47   发布时间:2023-06-13 13:46:45.0

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

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")
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")