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判断Abundant Number的方法,pythonabundant
- Python实现简单HTML表格解析的方法,pythonhtml表格解析
- Python本地与全局命名空间用法实例,python全局命名空间
- Python读取键盘输入的2种方法,python键盘输入2种
- Python实现比较两个列表(list)范围,pythonlist
- Python 3.x 新特性及10大变化,python3.x
- 搞笑的程序猿:看看你是哪种Python程序员,python程序员
- Python中unittest模块做UT(单元测试)使用实例,pythonun
- Windows下实现Python2和Python3两个版共存的方法,python2py
- Python2中的raw_input() 与 input(),python2raw_input
评论关闭