python词频统计实现思路_python实现词频统计

import string path1='/Users/Administrator/Documents/Walden.txt' path2='/Users/Administrator/Documents/result.txt' with open(path1,'r',encoding='UTF-8') as text,open(path2, 'w',encoding='UTF-8')as file: words = [raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()] words_index = set(words) counts_dict = {index: words.count(index) for index in words_index} for word in sorted(counts_dict, key=lambda x: counts_dict[x], reverse=True): file.write('{} -- {} times \n'.format(word,counts_dict[word])) file.close()

其中,path1,path2为路径。

注意:with open(path,'r',encoding='UTF-8') as text:encoding='UTF-8'必不可少,否则,读取txt文本出现“ 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence”错误。

你可能感兴趣的:(python词频统计实现思路)