python统计文本文件内单词数量的方法


Posted in Python onMay 30, 2015

本文实例讲述了python统计文本文件内单词数量的方法。分享给大家供大家参考。具体实现方法如下:

# count lines, sentences, and words of a text file
# set all the counters to zero
lines, blanklines, sentences, words = 0, 0, 0, 0
print '-' * 50
try:
 # use a text file you have, or google for this one ...
 filename = 'GettysburgAddress.txt'
 textf = open(filename, 'r')
except IOError:
 print 'Cannot open file %s for reading' % filename
 import sys
 sys.exit(0)
# reads one line at a time
for line in textf:
 print line,  # test
 lines += 1
 if line.startswith('\n'):
  blanklines += 1
 else:
  # assume that each sentence ends with . or ! or ?
  # so simply count these characters
  sentences += line.count('.') + line.count('!') + line.count('?')
  # create a list of words
  # use None to split at any whitespace regardless of length
  # so for instance double space counts as one space
  tempwords = line.split(None)
  print tempwords # test
  # word total count
  words += len(tempwords)
textf.close()
print '-' * 50
print "Lines   : ", lines
print "Blank lines: ", blanklines
print "Sentences : ", sentences
print "Words   : ", words
# optional console wait for keypress
from msvcrt import getch
getch()

希望本文所述对大家的python程序设计有所帮助。

Python 相关文章推荐
如何解决django配置settings时遇到Could not import settings 'conf.local'
Nov 18 Python
Python标准库defaultdict模块使用示例
Apr 28 Python
Python利用Beautiful Soup模块搜索内容详解
Mar 29 Python
详解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别
Jun 23 Python
Python通过Pygame绘制移动的矩形实例代码
Jan 03 Python
python提取具有某种特定字符串的行数据方法
Dec 11 Python
Python提取特定时间段内数据的方法实例
Apr 01 Python
Python中xml和dict格式转换的示例代码
Nov 07 Python
新手学python应该下哪个版本
Jun 11 Python
Python数据相关系数矩阵和热力图轻松实现教程
Jun 16 Python
Python 获取异常(Exception)信息的几种方法
Dec 29 Python
python UDF 实现对csv批量md5加密操作
Jan 01 Python
python使用win32com库播放mp3文件的方法
May 30 #Python
基于wxpython开发的简单gui计算器实例
May 30 #Python
python图像处理之镜像实现方法
May 30 #Python
python图像处理之反色实现方法
May 30 #Python
python中字典(Dictionary)用法实例详解
May 30 #Python
python集合用法实例分析
May 30 #Python
基于wxpython实现的windows GUI程序实例
May 30 #Python
You might like
php 301转向实现代码
2008/09/18 PHP
PHP中操作ini配置文件的方法
2013/04/25 PHP
php数组(array)输出的三种形式详解
2013/06/05 PHP
PHP把数字转成人民币大写的函数分享
2014/06/30 PHP
php封装db类连接sqlite3数据库的方法实例
2017/12/19 PHP
jQuery使用一个按钮控制图片的伸缩实现思路
2013/04/19 Javascript
jQuery解决下拉框select设宽度时IE 6/7/8下option超出显示不全
2013/05/27 Javascript
js网页版计算器的简单实现
2013/07/02 Javascript
jquery next nextAll nextUntil siblings的区别介绍
2013/10/05 Javascript
js 阻止子元素响应父元素的onmouseout事件具体实现
2013/12/23 Javascript
使用javascript控制cookie显示和隐藏背景图
2014/02/12 Javascript
让alert不出现弹窗的两种方法
2014/05/18 Javascript
js同源策略详解
2015/05/21 Javascript
JavaScript中标识符提升问题
2015/06/11 Javascript
js中获取时间new Date()的全面介绍
2016/06/20 Javascript
从零学习node.js之express入门(六)
2017/02/25 Javascript
基于JavaScript实现的希尔排序算法分析
2017/04/14 Javascript
深入理解React中何时使用箭头函数
2017/08/23 Javascript
从源码看angular/material2 中 dialog模块的实现方法
2017/10/18 Javascript
webstorm添加*.vue文件支持
2018/05/08 Javascript
elementUI select组件默认选中效果实现的方法
2019/03/25 Javascript
通过angular CDK实现页面元素拖放的步骤详解
2020/07/01 Javascript
[03:37]2014DOTA2国际邀请赛 主赛事第一日胜者组TOPPLAY
2014/07/19 DOTA
让IE6支持css3,让 IE7、IE8 都支持CSS3
2011/10/09 HTML / CSS
css3进阶之less实现星空动画的示例代码
2019/09/10 HTML / CSS
Hotels.com日本:国外和海外住宿,酒店预订
2019/12/13 全球购物
《散步》教学反思
2014/03/02 职场文书
货车司机岗位职责
2014/03/18 职场文书
大专毕业生求职信
2014/07/05 职场文书
校车安全责任书
2014/08/25 职场文书
仓管员岗位职责
2015/02/03 职场文书
药品销售内勤岗位职责
2015/04/13 职场文书
三严三实·严以用权心得体会
2016/01/12 职场文书
2016计划生育先进个人事迹材料
2016/02/29 职场文书
班干部竞选演讲稿(精选5篇)
2019/09/24 职场文书
浅谈Go语言多态的实现与interface使用
2021/06/16 Golang