2016-07-07 36 views
2

我正試圖'教'自己的Python。目前我正在使用Udacity提供的免費Python課程。我也在閱讀HTLPTHW。Python:檢查列表中的任何單詞是否出現在文檔中

其中一個模塊有點過時,並要求您爲現在不存在的網站使用URLLIB模塊。它所做的是「真/假」,根據給定文件中是否存在詛咒字。它引用該文件,在讀入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,如果它總是缺席,否則存在?

回答

0

讓我們試着明確地做到這一點:

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開發人員可以重寫它在更短的線路,但神奇地在這樣做之前,你必須學會​​如何做到這一點明確: )

0

既然你已經閱讀文件的內容read_text()你不必在check_profanity()

而且再次讀取該文件,行if str(Word_db) in quotes.read():轉換列表爲字符串,並檢查它是否是prese nt在文件中。它等效於:

if '["F***","S***","A**"]' in quotes.read()

您需要檢查是否列表中的任何元素出現在文件中。這可以使用for循環完成。

def check_profanity(text_to_check): 
     Word_db = ["bad","verybad"] 
     for word in Word_db: 
      if word in text_to_check: 
       print("Profanity Alert!!") 
       break 
     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.") 
相關問題