如何统计一段英文中的单词的个数

import re
import configparser
test_dict = {}
test_list = []
with open("testfile","r",encoding="utf-8") as f:
    for line in f:
        temp = re.findall("\w+",line)
        for i in temp:
            test_list.append(i)


for i in test_list:
    if i in test_dict.keys():
        count_i = test_dict[i] + 1
        test_dict[i] = count_i

    else:
        test_dict[i] = 1

config = configparser.ConfigParser()

config.read("config.ini")
for i in test_dict.keys():

    config[i.lower()] = {
        "单词全称":i.lower(),
        "出现的次数":test_dict[i]
    }
config.write(open("config.ini","w",encoding="utf-8"))
while True:
    inp = input("请输入要查询的单词:")
    config.read("config.ini",encoding="utf-8")
    if config.has_section(inp.lower()):
        count_i = config[inp.lower()]["出现的次数"]
        print("{name:s}出现的次数是{count:s}".format(name = inp,count = count_i))
    else:
        print("{name:s}不存在".format(name = inp))

  

 

配置文件的截图如下

如何统计一段英文中的单词的个数_第1张图片

 

转载于:https://www.cnblogs.com/bainianminguo/p/8385116.html

你可能感兴趣的:(如何统计一段英文中的单词的个数)