python實現文本中單詞個數統計

from operator import itemgetter

file_name = "F:\\python.txt"
line_counts = 0
word_counts = 0
character_counts = 0
count = {}
word = ""

with open(file_name, 'r') as f:
    for line in f:
        words = line.split()
        for index,word in enumerate(words):
            if word in count:
                count[word] += 1
            else:
                count[word] = 1
        line_counts += 1
        word_counts += len(words)
        character_counts += len(line)
    for index, item in count.items():
        print("單詞",index,"個數=",item)

print("行數 ", line_counts)
print("單詞的個數 ", word_counts)
print("字節數 ", character_counts)
相關文章
相關標籤/搜索