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

第 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 = 0
	words = 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


你可能感兴趣的:(python,统计单词个数,python)