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 相关文章推荐
简化Python的Django框架代码的一些示例
Apr 20 Python
python 使用get_argument获取url query参数
Apr 28 Python
windows环境下tensorflow安装过程详解
Mar 30 Python
使用Python开发SQLite代理服务器的方法
Dec 07 Python
对Python中DataFrame选择某列值为XX的行实例详解
Jan 29 Python
pytorch中的embedding词向量的使用方法
Aug 18 Python
Python编写打字训练小程序
Sep 26 Python
使用pyqt 实现重复打开多个相同界面
Dec 13 Python
pytorch torch.nn.AdaptiveAvgPool2d()自适应平均池化函数详解
Jan 03 Python
python给图像加上mask,并提取mask区域实例
Jan 19 Python
13个Pandas实用技巧,助你提高开发效率
Aug 19 Python
Python wordcloud库安装方法总结
Dec 31 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与javascript对多项选择的处理
2006/10/09 PHP
PHP简介
2006/10/09 PHP
php下的权限算法的实现
2007/04/28 PHP
PHP上传图片进行等比缩放可增加水印功能
2014/01/13 PHP
discuz目录文件资料汇总
2014/12/30 PHP
PHP上传图片时判断上传文件是否为可用图片的方法
2016/10/20 PHP
PHP房贷计算器实例代码,等额本息,等额本金
2017/04/01 PHP
artdialog的图片/标题以及关闭按钮不显示的解决方法
2013/06/27 Javascript
jQuery实现tag便签去重效果的方法
2015/01/20 Javascript
js窗口关闭提示信息(兼容IE和firefox)
2015/10/23 Javascript
jQuery中通过ajax调用webservice传递数组参数的问题实例详解
2016/05/20 Javascript
javascript 实现文本使用省略号替代(超出固定高度的情况)
2017/02/21 Javascript
Vue中的$set的使用实例代码
2018/10/08 Javascript
Webpack中loader打包各种文件的方法实例
2019/09/03 Javascript
javascript将扁平的数据转为树形结构的高效率算法
2020/02/27 Javascript
[01:50]WODOTA制作 DOTA2中文宣传片《HERO》
2013/04/28 DOTA
python插入排序算法的实现代码
2013/11/21 Python
在Mac OS上部署Nginx和FastCGI以及Flask框架的教程
2015/05/02 Python
Python中的多行注释文档编写风格汇总
2016/06/16 Python
python好玩的项目—色情图片识别代码分享
2017/11/07 Python
python实现决策树
2017/12/21 Python
大家都说好用的Python命令行库click的使用
2019/11/07 Python
python实现PDF中表格转化为Excel的方法
2020/06/16 Python
python 调用API接口 获取和解析 Json数据
2020/09/28 Python
香港时装购物网站:ZALORA香港
2017/04/23 全球购物
选购世界上最好的美妆品:Cult Beauty
2017/11/03 全球购物
Europcar澳大利亚官网:全球汽车租赁领域的领导者
2019/03/24 全球购物
八年级美术教学反思
2014/02/02 职场文书
房地产财务部员工岗位职责
2014/03/12 职场文书
项目经理任命书
2014/06/04 职场文书
2014市国税局对照检查材料思想汇报
2014/09/23 职场文书
文员转正自我鉴定怎么写
2014/09/29 职场文书
2014年党风廉政建设工作总结
2014/11/19 职场文书
公司车队管理制度
2015/08/04 职场文书
2019年销售人员的职业生涯规划书
2019/03/25 职场文书
Mac M1安装mnmp (Mac+Nginx+MySQL+PHP) 开发环境
2021/03/29 PHP