2016-05-04 87 views
0

使用一個簡單的腳本來搜索某個文件中的關鍵字。一旦找到關鍵字,就會計算它使用的次數,然後將每次找到關鍵字的行號記錄到列表中。然後打印出它被發現的次數和行數。 該程序計算關鍵字罰款,但它被發現一次後,它會進入下一行。它不會計算多個關鍵字。我知道它是這樣做的,因爲if語句:if keyword in line: Number_Of_Key_Words = Number_Of_Key_Words + 1 found_at.append(num) continuePython 3在文本文件中每行搜索關鍵字不止一次

如何獲得它以說明每行文件可能存在多個關鍵字?

全碼:

def search(): 
Number_Of_Key_Words = 0 
found_at = []; 
keyword = input("Enter a key word to search for: ") 
with open("WordList.txt") as file: 
    for num, line in enumerate(file, 1): 
     if keyword in line: 
      Number_Of_Key_Words = Number_Of_Key_Words + 1 
      found_at.append(num) 
      continue 
    print(Number_Of_Key_Words) 
    print("Found on lines: ", found_at) 
search() 

例 單詞列表

字逐字

搜索 「字」 輸出:1
上系中發現[1]

想要: 輸出:3
找到線路[1]

回答

1

一種方法是使用re模塊findall,它會查找字符串中的所有匹配項。

像這樣的東西(我也提出了一些修改):

import re 

keyword = input("...") 
found_at = [] 
counter = 0 # Number_Of_Key_Words is not a good python name 
# file is a PY2 built in, so I use 'f' instead 
# also you should be explicit for the open mode, 'r' == read mode 
with open("...", "r") as f: 
    for num, line in enumerate(f, 1): 
     # re.findall() will return a list of all keyword occurrence 
     # len() will then measure the occurrence effectively 
     count = len(re.findall(keyword, line)) 
     if count > 0: # ie. keyword in line at least once 
      found_at.append(num) 
      counter += count 

    print(found_at) 
    print(counter) 

希望這有助於。

+0

@Tommy ,就像dreamzboy使用字符串'count()'的答案一樣,這也是一個好主意,但是如果你需要搜索關鍵字而忽略個案或者模式匹配等,你可能從're'模塊中獲得更多的好處,請閱讀[docs here]( https://docs.python.org/3/library/re.html) 。你應該探索並嘗試更多,並從中學習。 – Anzel

1

備選地,可以使用 「str.count(關鍵字,開始,結束)」

樣品數據 「wordtext.txt」:

red, blue, red, green. 
blue, yellow, white. 
green, orange. 
red, blue, green, red, black, yellow, red. 

輸出:

>>> with open ('wordtext.txt') as f: 
    for i, line in enumerate (f): 
     found = line.count ('red') 
     if found: 
      print ('Line: %d Red: %d' % (i, found)) 


Line: 0 Red: 2 
Line: 3 Red: 3 
>>> 
+0

我不得不承認're.findall()'答案是我的第一個傾向,但這當然更優雅。 –

+0

需要修正枚舉的開始位置,否則行會被偏移1.同時最好在文件讀取模式下明確 – Anzel

+0

RE模塊確實非常強大,但爲什麼在不需要時導入呢? =) – dreamzboy

相關問題