标签:read 创建 资源 一个 txt tps text strip 换行
GitHub地址:FightingBob 【Give me a star , thanks.】
1 import string 2 from os import path 3 with open(‘瓦尔登湖(英文版).txt‘,‘rb‘) as text1: 4 words = [word.strip(string.punctuation).lower() for word in str(text1.read()).split()] 5 words_index = set(words) 6 count_dict = {index:words.count(index) for index in words_index} 7 with open(path.dirname(__file__) + ‘/file1.txt‘,‘a+‘) as text2: 8 text2.writelines(‘以下是词频统计的结果:‘ + ‘\n‘) 9 for word in sorted(count_dict,key=lambda x:count_dict[x],reverse=True): 10 text2.writelines(‘{}--{} times‘.format(word,count_dict[word]) + ‘\n‘) 11 text1.close() 12 text2.close()
1 with open(‘瓦尔登湖(英文版).txt‘,‘rb‘) as text1:
GitHub地址:FightingBob 【Give me a star , thanks.】
标签:read 创建 资源 一个 txt tps text strip 换行
原文地址:https://www.cnblogs.com/littlebob/p/9189794.html