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))
配置文件的截图如下