今天学习python,用其做了一个统计单词频数的小例子。
#coding=utf-8
import string
path = 'D:/Walden.txt'with
open(path,'r') as text:
words = [raw_words.strip(string.punctuation).lower() for raw_words in text.read().split()]
words_index = set(words)
counts_dist = {index : words.count(index) for index in words_index}
file = open('D:/wordCount.txt','w')
for word in sorted(counts_dist,key=lambda x:counts_dist[x],reverse=True):
file.write('{}-{} times'.format(word,counts_dist[word]))
file.close()
print('done')
运行时,报了一个编码错误:
UnicodeEncodeError: 'gbk' codec can't encode character '\ufeff' in position 0: illegal multibyte sequence
查了一下,发现是因为遇到了非法字符,因此在转码的过程中出现了异常,修改默认的编码为utf-8其实就可以了,然后开始查如何设置编码,最后发现如下设置:
open(path,'r','utf-8')
即可