|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import string
path = 'C:\\Users\\lenovo\\Desktop\\Walden.txt'
with open(path, 'r') as text:
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):
print('{} -- {} time'.format(word,counts_dict[word]))
Traceback (most recent call last):
File "F:/1/first.py", line 4, in <module>
words = [raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]
UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence
with open(path, 'r', encoding='utf8') as text:
|
|