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)


你可能感兴趣的:(python)