2015-10-20 53 views
1
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

+0

你是如何調用該函數迭代之前刪除一切嗎? –

+0

對不起,我錯過了最後一段代碼。我編輯了原文。 – neacal

+0

調用函數後,你在哪裏檢查輸出? –

回答

0

由於output = list()位於代碼的頂層,並且不在函數內部,因此它被視爲全局變量。 要編輯函數中的全局變量,必須先使用global關鍵字。

例子:

gVar = 10 

def editVar(): 
    global gVar 
    gVar += 5 

所以編輯變量output的功能find_word()內必須爲其指定值之前,鍵入global output

它應該是這樣的:

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 

     global output 
     if keyword.lower() in words: 
      output.append(line) 

    return output 

在未來,試圖從全局變量敬而遠之,除非你絕對需要他們。他們會變得凌亂!

+0

錯誤。您可以在不使用'global'的情況下調用全局變量的變異方法。當你想把*賦給全局變量時,你只需要'global',否則賦值就會創建一個同名的局部變量。 –

+0

有趣的一點。我無法解釋爲什麼這個解決方案爲neacal工作。有任何想法嗎? – pyInProgress

+0

取決於他使用該方法。他返回列表並具有全局定義。如果他在方法中添加「全局」,那麼他可以在全局對象上看到這些更改。但是,如果他使用返回值,他不需要添加'global'。 – Kody

2

請使用正則表達式。請參閱Regex in Python的一些文檔。替換每個字符/字符集是令人困惑的。使用列表和.append()看起來是正確的,但也許要考慮在for循環內調試line變量,偶爾打印它以確保其值是您想要的值。

通過pyInProgress的回答讓有關全局變量的好點,雖然沒有測試它,我不相信如果output返回變量使用全局變量output,而不是它是必需的。如果您需要更多關於全局變量的信息,請參閱this StackOverflow post

1

遍歷string.punctuation通過線

import string, re 

r = ',' 

def find_word(filepath, keyword): 

    output = [] 
    with open(filepath, 'rb') as f: 
     data = f.read() 
     for x in list(string.punctuation): 
      if x != r: 
       data = data.replace(x, '') 
     data = re.sub(r',{2,}', r, data, re.M).splitlines() 

    for i, line in enumerate(data): 
     if keyword.lower() in line.lower().split(r): 
      output.append((i, line)) 
    return output 

print find_word('pg844.txt', 'and') 
+0

更新到逗號分割 –

+0

是的,如果x!= r:' –

+0

你會怎麼做,而不是使用Regex? – Kody