#定义函数
def getFileText():
filTxt=open(r'..\Stu_pack\file\See You Again.txt','r').read()
#将文本里所有的字母都转换成小写
filTxt=filTxt.lower()
#将所有的标点符号都用空格替代
for ch in '!"#$%&()*+-*/,.;;<=>?@[]\\^_{}|~':
filTxt=filTxt.replace(ch," ")
return filTxt
#将只有带空格的文本按空格分成每个单词
letterTxt=getFileText()
words=letterTxt.split()
#定义字典变量已备记录单词出现的次数和最后输入的内容
wdCountDict={}
excludes={"the","you","your","that","will"}
#excludes={"the","of","you","your","that","will","don't"}
#获取某单词的拼写和累加出现的次数
for word in words:
wdCountDict[word]=wdCountDict.get(word,0)+1
#将exclude里出现的单词删除掉
for word in excludes:
del(wdCountDict[word])
#将字典数据转化成列表类型
items=list(wdCountDict.items())
#将列表按第2列x[1]降序排列
items.sort(key=lambda x:x[1],reverse=True)
#将结果内容以对应的格式打印出来
print("{0:<10}{1:>5}".format("word","count"))
print("_"*21)
for key,val in items:
if len(key)>3 and val>4:
#‘<'表示左对齐'>’表示右对其
print("{0:<10}{1:>5}".format(key,val))