Python计算一个文件里字数的方法


Posted in Python onJune 15, 2015

本文实例讲述了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 相关文章推荐
python超简单解决约瑟夫环问题
May 12 Python
python从入门到精通(DAY 2)
Dec 20 Python
Python matplotlib绘图可视化知识点整理(小结)
Mar 16 Python
tensorflow 使用flags定义命令行参数的方法
Apr 23 Python
Python实现朴素贝叶斯分类器的方法详解
Jul 04 Python
对Python3 pyc 文件的使用详解
Feb 16 Python
详解pandas使用drop_duplicates去除DataFrame重复项参数
Aug 01 Python
Django应用程序入口WSGIHandler源码解析
Aug 05 Python
pytorch打印网络结构的实例
Aug 19 Python
python单向链表的基本实现与使用方法【定义、遍历、添加、删除、查找等】
Oct 24 Python
使用Keras建立模型并训练等一系列操作方式
Jul 02 Python
python 常用日期处理-- datetime 模块的使用
Sep 02 Python
Python素数检测实例分析
Jun 15 #Python
Python计算三维矢量幅度的方法
Jun 15 #Python
Python栈类实例分析
Jun 15 #Python
Python实现股市信息下载的方法
Jun 15 #Python
给Python入门者的一些编程建议
Jun 15 #Python
Python修改MP3文件的方法
Jun 15 #Python
Python从MP3文件获取id3的方法
Jun 15 #Python
You might like
10个实用的PHP代码片段
2011/09/02 PHP
php的crc32函数使用时需要注意的问题(不然就是坑)
2015/04/21 PHP
PHP中余数、取余的妙用
2015/06/29 PHP
yii分页组件用法实例分析
2015/12/28 PHP
php.ini中date.timezone设置详解
2016/11/20 PHP
jquery 图片 上一张 下一张 链接效果(续篇)
2010/04/20 Javascript
15 个 JavaScript Web UI 库
2010/05/19 Javascript
实现web打印的各种方法介绍及实现代码
2013/01/09 Javascript
用js实现控件的隐藏及style.visibility的使用
2013/06/14 Javascript
javascript读写json示例
2014/04/11 Javascript
jquery实现用户打分评分特效
2015/05/28 Javascript
angularjs实现多张图片上传并预览功能
2017/02/24 Javascript
Node.js中 __dirname 的使用介绍
2017/06/19 Javascript
微信{"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
2018/10/12 Javascript
使用localStorage替代cookie做本地存储
2019/09/25 Javascript
[57:29]Alliance vs KG 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/17 DOTA
[01:18:45]DOTA2-DPC中国联赛 正赛 DLG vs Dragon BO3 第三场2月1日
2021/03/11 DOTA
python实现中文输出的两种方法
2015/05/09 Python
Python制作数据导入导出工具
2015/07/31 Python
回调函数的意义以及python实现实例
2017/06/20 Python
Python调用C# Com dll组件实战教程
2017/10/12 Python
python使用正则表达式的search()函数实现指定位置搜索功能
2017/11/10 Python
Python实现接受任意个数参数的函数方法
2018/04/21 Python
Django项目中实现使用qq第三方登录功能
2019/08/13 Python
Python绘制热力图示例
2019/09/27 Python
numpy数组做图片拼接的实现(concatenate、vstack、hstack)
2019/11/08 Python
使用css3制作动感导航条示例
2014/01/26 HTML / CSS
澳大利亚领先的在线美容商店:Facial Co
2017/10/22 全球购物
Hotels.com加拿大:领先的在线住宿网站
2018/10/05 全球购物
优秀团员个人事迹材料
2014/01/29 职场文书
《地震中的父与子》教学反思
2014/04/10 职场文书
老师对学生的评语
2014/04/18 职场文书
校园环保广播稿(3篇)
2014/09/15 职场文书
公证书格式
2015/01/23 职场文书
工商行政处罚决定书
2015/06/24 职场文书
大学校园餐饮创业计划书
2019/08/07 职场文书