r = ","
x = ""
output = list()
import string
def find_word(filepath,keyword):
doc = open(filepath, 'r')
for line in doc:
#Remove all the unneccessary characters
line = line.replace("'", r)
line = line.replace('`', r)
line = line.replace('[', r)
line = line.replace(']', r)
line = line.replace('{', r)
line = line.replace('}', r)
line = line.replace('(', r)
line = line.replace(')', r)
line = line.replace(':', r)
line = line.replace('.', r)
line = line.replace('!', r)
line = line.replace('?', r)
line = line.replace('"', r)
line = line.replace(';', r)
line = line.replace(' ', r)
line = line.replace(',,', r)
line = line.replace(',,,', r)
line = line.replace(',,,,', r)
line = line.replace(',,,,,', r)
line = line.replace(',,,,,,', r)
line = line.replace(',,,,,,,', r)
line = line.replace('#', r)
line = line.replace('*', r)
line = line.replace('**', r)
line = line.replace('***', r)
#Make the line lowercase
line = line.lower()
#Split the line after every r (comma) and name the result "word"
words = line.split(r)
#if the keyword (also in lowercase form) appears in the before created words list
#then append the list output by the whole line in which the keyword appears
if keyword.lower() in words:
output.append(line)
return output
print find_word("pg844.txt","and")
這段代碼的目標是搜索某個關鍵字的文本文件,例如「and」,然後將找到該關鍵字的整行放入列表中類型(int,string)。 int應該是行號和上面提到的剩餘整行的字符串。追加列表後的空輸出
我還在研究行號 - 所以現在還沒有問題。但問題是:輸出是空的。即使我追加一個隨機字符串而不是行,我也沒有得到任何結果。
如果我使用
if keyword.lower() in words:
print line
我得到了所有需要的線路,其中的關鍵字出現。但我無法將它輸入到輸出列表中。
的文本文件,我想通過搜索:http://www.gutenberg.org/cache/epub/844/pg844.txt
你是如何調用該函數迭代之前刪除一切嗎? –
對不起,我錯過了最後一段代碼。我編輯了原文。 – neacal
調用函數後,你在哪裏檢查輸出? –