【问题描述】 编写程序统计一个英文文本文件中每个单词的出现次数(词频统计),并将统计结果按单词出现频率由高至低输出到指定文件中。 注:在此单词为仅由字母组成的字符序列。包含大写字母的单词应将大写字母转

【样例输入】若文件article.txt中内容如下:
I will give you some advice about life.
Eat more roughage;
Do more than others expect you to do and do it pains;
Remember what life tells you;
do not take to heart every thing you hear.
do not spend all that you have.
do not sleep as long as you want.
【样例输出】则程序运行后文件wordfreq.txt中内容如下:
do 6
you 6
not 3
as 2
life 2
more 2
to 2
about 1
advice 1
all 1…


import operator
import string
import linecache
'''stopword = ‘’
str = ‘’
for line in iter(input,stopword):
str+=line
print(str)
'''
speech=''
fo=open('C:/Users/24224/Documents/Python Scripts/article.txt','r')
for line in fo.readlines() :
    speech+=line
#print(speech)
fo.close()
spe=speech.lower().split()
dic={}
for word in spe:
    if word not in dic:
        dic [word]=1
    else:
        dic [word]= dic[word]+1
swd=sorted(dic.items(),key=operator.itemgetter(1),reverse=True)

fo1=open('C:/Users/24224/Documents/Python Scripts/wordfreq.txt','w')
for i ,j in swd:
    fo1.writelines(i+' '+str(j))
    fo1.writelines('\n')

你可能感兴趣的:(【问题描述】 编写程序统计一个英文文本文件中每个单词的出现次数(词频统计),并将统计结果按单词出现频率由高至低输出到指定文件中。 注:在此单词为仅由字母组成的字符序列。包含大写字母的单词应将大写字母转)