Python计算一个文件里字数的方法
更新时间:2015年06月15日 14:37:39 作者:小卒过河
这篇文章主要介绍了Python计算一个文件里字数的方法,涉及Python文件操作及内容遍历的相关技巧,需要的朋友可以参考下
本文实例讲述了Python计算一个文件里字数的方法。分享给大家供大家参考。具体如下:
这段程序从所给文件中找出字数来。
from string import *
def countWords(s):
words=split(s)
return len(words)
#returns the number of words
filename=open("welcome.txt",'r')
#open an file in reading mode
total_words=0
for line in filename:
total_words=total_words + countWords(line)
#counts the total words
print total_words
希望本文所述对大家的Python程序设计有所帮助。
相关文章
Python中内置数据类型list,tuple,dict,set的区别和用法
这篇文章主要给大家介绍了Python中内置数据类型list,tuple,dict,set的区别和用法,都是非常基础的知识,十分的细致全面,有需要的小伙伴可以参考下。2015-12-12
解决python matplotlib imshow无法显示的问题
今天小编就为大家分享一篇解决python matplotlib imshow无法显示的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-05-05


最新评论