python统计文件中文字数量的方法,python统计,虽然python自带有c


虽然python自带有count()统计方法,但我还是自己动手写了一个方法来统计文件中的字数量。

自己写的python统计文件中字数量的方法,源码如下(供参考):需要首先导入python string模块中的方法。

from string import *def countWords(s):    words=split(s)    return len(words)       #returns the number of wordsfilename=open("welcome.txt",'r')    #open an file in reading mode#www.iplaypy.comtotal_words=0for line in filename:    total_words=total_words + countWords(line)      #counts the total wordsprint total_words

相关文章推荐:Python统计代码行数的快捷方法

编橙之家文章,

评论关闭