Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查Python中的文本文件中是否有单词_Python - Fatal编程技术网

检查Python中的文本文件中是否有单词

检查Python中的文本文件中是否有单词,python,Python,我不知道为什么我的代码不能工作。有人能帮忙吗 userinp = str(input("What is wrong with your phone?\n")) with open("keywords.txt", "r") as solution: for line in solution: if userinp in line: print("in list") else: print("not in lis

我不知道为什么我的代码不能工作。有人能帮忙吗

userinp = str(input("What is wrong with your phone?\n"))
with open("keywords.txt", "r") as solution:
    for line in solution:
        if userinp in line:
            print("in list")
        else:
            print("not in list")
我的“keywords.txt”只包含单词wet

每次我把“湿”放在空闲的时候,它都会说它在列表中。然而,当我把“手机是湿的”,它说“不在名单上”


感谢您的帮助

我运行了与keywords.txt文件中的某些行内容完全相同的代码:-

userinp=strinput您的手机怎么了?\n 使用openkeywords.txt,r作为解决方案: 对于在线解决方案: 如果userinp在行中: 打印输入列表 其他: 打印不在列表中

效果绝对不错。我注意到的唯一错误是在使用Python2.7时输入,除了输出很好之外。每次迭代后,它都会打印出是否符合要求


根据您的最新更新,当txt文件中有'wet'时,您搜索'phone is wet',很明显,这句话不在该行中!然而,另一种方法会奏效

with线以下的所有内容都应缩进。为什么你有一行只写了for?我编辑了代码的实际外观是否有任何错误?看起来你有一些东西可以打印在列表中,或者不打印在列表中,用于文件中的每一行。如果它不是您想要的,请描述您遇到的问题或错误。请编辑以指定问题