2016-07-31 43 views
1

我試圖計算單詞'the'出現在保存爲文本文件的兩本書中的次數。我正在運行的代碼會爲每本書返回零。使用count方法來計算文本文件中的某個單詞

這裏是我的代碼:

def word_count(filename): 
    """Count specified words in a text""" 
    try: 
     with open(filename) as f_obj: 
      contents = f_obj.readlines() 
      for line in contents: 
       word_count = line.lower().count('the') 
      print (word_count) 

    except FileNotFoundError: 
     msg = "Sorry, the file you entered, " + filename + ", could not be  found." 
    print (msg) 

dracula = 'C:\\Users\\HP\\Desktop\\Programming\\Python\\Python Crash Course\\TEXT files\\dracula.txt' 
siddhartha = 'C:\\Users\\HP\\Desktop\\Programming\\Python\\Python Crash Course\\TEXT files\\siddhartha.txt' 

word_count(dracula) 
word_count(siddhartha) 

我到底錯在這裏做什麼?

+0

不。我嘗試使用你的線增加,但我必須分配word_count之前我增加它。所以我添加了第二行增加word_count與它本身,它仍然給我零這兩本書。 –

回答

1

除非單詞'the'出現在每個文件的最後一行,否則您將看到零。

你可能要初始化的變量word_count零,則使用增強加法(+=):

例如:

def word_count(filename): 
    """Count specified words in a text""" 
    try: 
     word_count = 0          # <- change #1 here 
     with open(filename) as f_obj: 
      contents = f_obj.readlines() 
      for line in contents: 
       word_count += line.lower().count('the')  # <- change #2 here 
      print(word_count) 

    except FileNotFoundError: 
     msg = "Sorry, the file you entered, " + filename + ", could not be  found." 
    print(msg) 

dracula = 'C:\\Users\\HP\\Desktop\\Programming\\Python\\Python Crash Course\\TEXT files\\dracula.txt' 
siddhartha = 'C:\\Users\\HP\\Desktop\\Programming\\Python\\Python Crash Course\\TEXT files\\siddhartha.txt' 

word_count(dracula) 
word_count(siddhartha) 

增強除了是沒有必要的,只是有幫助的。這條線:

word_count += line.lower().count('the') 

可以寫成

word_count = word_count + line.lower().count('the') 

但你也並不需要一次讀取的所有行到內存中。您可以從文件對象中直接遍歷行。例如:

def word_count(filename): 
    """Count specified words in a text""" 
    try: 
     word_count = 0 
     with open(filename) as f_obj: 
      for line in f_obj:      # <- change here 
       word_count += line.lower().count('the') 
     print(word_count) 

    except FileNotFoundError: 
     msg = "Sorry, the file you entered, " + filename + ", could not be  found." 
     print(msg) 

dracula = 'C:\\Users\\HP\\Desktop\\Programming\\Python\\Python Crash Course\\TEXT files\\dracula.txt' 
siddhartha = 'C:\\Users\\HP\\Desktop\\Programming\\Python\\Python Crash Course\\TEXT files\\siddhartha.txt' 

word_count(dracula) 
word_count(siddhartha) 
+0

謝謝jedwards ....那工作:) –

3

您正在爲每次迭代重新分配word_count。這意味着最後它將與文件最後一行中的出現次數the相同。你應該得到這筆錢。另一件事:應該there匹配?可能不會。您可能要使用line.split()。此外,您可以直接遍歷文件對象;不需要.readlines()。最後,使用生成器表達式來簡化。我的第一個例子是沒有生成器表達式;第二個是與它:

def word_count(filename): 
    with open(filename) as f_obj: 
     total = 0 
     for line in f_obj: 
      total += line.lower().split().count('the') 
     print(total) 
def word_count(filename): 
    with open(filename) as f_obj: 
     total = sum(line.lower().split().count('the') for line in f_obj) 
     print(total) 
0
import os 
def word_count(filename): 
    """Count specified words in a text""" 
    if os.path.exists(filename): 
     if not os.path.isdir(filename): 
      with open(filename) as f_obj: 
       print(f_obj.read().lower().count('t')) 
     else: 
      print("is path to folder, not to file '%s'" % filename) 
    else: 
     print("path not found '%s'" % filename) 
相關問題