簡體   English   中英

使用count方法對文本文件中的某個單詞進行計數

[英]using count method to count a certain word in text file

我正在嘗試計算單詞“ the”在另存為文本文件的兩本書中出現的次數。 我正在運行的代碼為每本書返回零。

這是我的代碼:

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)

我在這里做錯了什么?

您將為每次迭代重新分配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)

除非單詞“ 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)

其他方式:

with open(filename) as f_obj:
    contents = f_obj.read()
    print("The word 'the' appears " + str(contents.lower().count('the')) + " times")
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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM