首页 > 解决方案 > 如何使用 Python 制作字数计数器程序?

问题描述

我需要创建一个读取文本文件并打印以下内容的程序:

  1. 文本中的所有唯一词
  2. 它们在文本中出现的次数
  3. 总字数

from collections import Counter
count = 0

file = open(r"sample_input.txt", "r", encoding="utf-8-sig")
wordcount = Counter(file.read().split())

for item in wordcount.items():
    print("{}\t-\t{}".format(*item))

输出应如下所示:

WORD FREQUENCY 
can - 1
grow - 1
and - 1
shrink - 1
on - 1
demand - 1 
TOTAL = 6

我的程序以不同的方式计算小写和大写。有没有办法过滤掉标点符号?

标签: python

解决方案


创建单词列表时,通过str.lower将它们转换为小写

from collections import Counter

wordcount = Counter()

#Open the file
with open(r"sample_input.txt", "r", encoding="utf-8-sig") as file:

    #Iterate through each line
    for line in file:

        #Strip any trailing or leading whitespaces
        line = line.strip()
        #Iterate over the words and keep updating counter
        for word in line.split():
            wordcount.update([word.lower()])

for key, value in wordcount.items():
    print("{}\t-\t{}".format(key, value))

#Sum up the count of words
num_words = sum(wordcount.values())
print(num_words)

输出将是

can - 1
grow - 1
and - 1
shrink - 1
on - 1
demand - 1
6

推荐阅读