第三次作业
作者:互联网
from string import punctuationdef process_file(dst):try:f = open(dst)except IOError, s:print sreturn Nonetry:bvffer = f.read()except:print "Read File Error!"return Nonef.close()return bvffer
def process_buffer(bvffer):if bvffer:word_freq = {}for item in bvffer.strip().split():word = item.strip(punctuation+' ')if word in word_freq.keys():word_freq[word] += 1else:word_freq[word] = 1return word_freq
def output_result(word_freq):if word_freq:sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)for item in sorted_word_freq[:10]:print item
if __name__ == "__main__":import argparseparser = argparse.ArgumentParser()parser.add_argument('dst')args = parser.parse_args()dst = args.dstbvffer = process_file(dst)word_freq = process_buffer(bvffer)output_result(word_freq)
def process_buffer(bvffer):if bvffer:word_freq = {}for item in bvffer.strip().split():word = item.strip(punctuation+' ')if word in word_freq.keys():word_freq[word] += 1else:word_freq[word] = 1return word_freq
def output_result(word_freq):if word_freq:sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)for item in sorted_word_freq[:10]:print item
if __name__ == "__main__":import argparseparser = argparse.ArgumentParser()parser.add_argument('dst')args = parser.parse_args()dst = args.dstbvffer = process_file(dst)word_freq = process_buffer(bvffer)output_result(word_freq)
标签:__,word,第三次,process,dst,作业,bvffer,freq 来源: https://www.cnblogs.com/lyb123456/p/10617812.html