您的位置:首页 > 编程语言 > Python开发

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

2016-11-23 20:31 537 查看
第 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐