第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数

来源:互联网 发布:js字符串转为json对象 编辑:程序博客网 时间:2024/06/06 03:53

第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。


1、strip()没有参数时,删除空白符,包括\n \r \t 空格。strip() 函数只能用于str类型,list类型等不可用。

2、split()用于分割,分隔符可以自己制定

def word_counts(inputfile):""""""if os.path.isfile(inputfile) != True:print "inputfile not exists"sys.exit()word_count = 0words = open(inputfile, "r").readlines()for word in words:print ("word: %s" %word)temp = word.strip().split(' ')word_count += len(temp)print ("word count: %s" %word_count)return word_count


1 0