def getText():
#获取文件
text = open("Walden.txt","r").read()
text = text.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~' :
text = text.replace(ch," ")
return text
txt = getText()
words = txt.split()
counts = {} #定义空字典
for word in words:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
#将列表按照count中键值从大到小的顺序进行排列
items.sort(key=lambda x:x[1],reverse=True)
#输出结果,前二十位最高频词汇
for i in range(10):
word,count = items[i]
print("#{0:<10}{1:>5}".format(word,counts))
词频统计代码如上,但是出现报错,请问这是什么原因啊?