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