Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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_List_Keyword - Fatal编程技术网

Python:检查列表中的任何单词是否存在于文档中

Python:检查列表中的任何单词是否存在于文档中,python,list,keyword,Python,List,Keyword,我正在努力“自学”Python。目前我正在使用Udacity上提供的免费Python课程。我也在读HTLPTHW 其中一个模块有点过时,它要求您为一个现已不存在的网站使用URLLIB模块。它所做的是根据给定文档中是否存在诅咒词来说明True/False。它引用该文件,在读取URL搜索后输入其内容,然后在搜索后解析为True/False 我在想办法解决这个问题,我想我可以使用一个在文档中搜索的誓言列表。如果在打开的文档中还发现列表中的咒骂,它将发出警报 我遇到了一些问题,部分原因可能是我保留了基于

我正在努力“自学”Python。目前我正在使用Udacity上提供的免费Python课程。我也在读HTLPTHW

其中一个模块有点过时,它要求您为一个现已不存在的网站使用URLLIB模块。它所做的是根据给定文档中是否存在诅咒词来说明True/False。它引用该文件,在读取URL搜索后输入其内容,然后在搜索后解析为True/False

我在想办法解决这个问题,我想我可以使用一个在文档中搜索的誓言列表。如果在打开的文档中还发现列表中的咒骂,它将发出警报

我遇到了一些问题,部分原因可能是我保留了基于教程的大部分原始代码格式——这意味着很多代码可能是根据URLLIB方法定制的,而不是关键字搜索

def read_text():
    quotes = open("/Users/Ishbar/Desktop/movie_quotes.txt")
    contents_of_file = quotes.read()
    print(contents_of_file)
    quotes.close()
    check_profanity(contents_of_file)

def check_profanity(text_to_check):
    Word_db = ["F***","S***","A**"]
    quotes = open("/Users/Ishbar/Desktop/movie_quotes.txt")
    contents_of_file = quotes.read()
    output == Word_db
    if str(Word_db) in quotes.read():
        output == 1
    if output == 1:
        print("Profanity Alert!!")
    elif output == 0:
        print("This document has no curse words.")
    else:
        print("ERROR: Could not scan the document properly.")
read_text()
我就是不能让代码快乐。我要么总是被发现亵渎,要么没有发现亵渎。我想我可以让它修改输出是什么,输出的默认状态是不亵渎,除非另有发现


为此,如果亵渎/缺席总是缺席,那么我是否需要有一个elif来表示亵渎/缺席

让我们试着明确地做到这一点:

def check_profanity(document_to_check):
    Word_db = ["F***","S***","A**"]
    with open(document_to_check) as quotes:     # let's open the document
        for line in quotes:                     # parse it line by line
            for word in Word_db:                # check offensing words one by one
                if word in line:
                    return True                 # if found one bad word, go out :-)

if check_profanity("/Users/Ishbar/Desktop/movie_quotes.txt"):
    print "Profanity Alert!!"
else:
    print("This document has no curse words.")      

当然,一个有经验的python开发人员可以用更少的行重写它,但是在神奇地完成之前,您必须学习如何显式地完成:)

因为您已经在
read\u text()
中读取了文件的内容,所以不必在
check\u亵渎()
中再次读取文件

此外,quotes.read()中的行
if str(Word_db):
将列表转换为字符串,并检查文件中是否存在该字符串。这相当于:

如果引号中的“[“F***”,“S***”,“A**]”为空。read()

您需要检查文件中是否存在列表的任何元素。这可以使用
for
循环来完成

def check_profanity(text_to_check):
    Word_db = ["bad","verybad"]
    if set(Word_db).intersection(set(text_to_check.split())):
        print("Profanity Alert!!")
    else:
        print("This document has no curse words.")

check_profanity("this file contains bad words") # 1st call
check_profanity("this file contains good words") #2nd call
输出:

亵渎警告

这份文件没有咒语

您也可以使用正则表达式来实现这一点

import re
if re.search("("+")|(".join(Word_db)+")", quotes.read()):
   print("Profanity Alert!!")
else:
   print("This document has no curse words.")

我刚刚遇到了一个类似的问题(也在上udacity课程)。毫无疑问,不久前你自己也会从这一点出发,但这最终是我的解决方案。遵循gaganso,并使用.csv亵渎列表():


注意:同样使用Python3.6.5作为Python2的补充——udacity课程的另一个小问题!
    def read_text():
    text = open("twoSentences.txt")
    contents = text.read()
    #print(contents)
    return(str(contents))
    text.close()

    a_text = read_text()

    def check_curse(sample_text):
    curse_list = open("Terms-to-Block.csv")
    curse_words = str(curse_list.read())  
    sep_text = sample_text.split()
    sep_curses = curse_words.split()
    if set(sep_curses).intersection(set(sep_text)):
        print("ALERT")
    else:
        print("OK")

    check_curse(a_text)