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和C语言混合编程实例
Jun 04 Python
python实现查找excel里某一列重复数据并且剔除后打印的方法
May 26 Python
Python新手入门最容易犯的错误总结
Apr 24 Python
Python模拟鼠标点击实现方法(将通过实例自动化模拟在360浏览器中自动搜索python)
Aug 23 Python
基于Python函数和变量名解析
Jul 19 Python
微信小程序python用户认证的实现
Jul 29 Python
python中单下划线(_)和双下划线(__)的特殊用法
Aug 29 Python
python基于gevent实现并发下载器代码实例
Nov 01 Python
详解Python利用configparser对配置文件进行读写操作
Nov 03 Python
python中用ggplot绘制画图实例讲解
Jan 26 Python
Python3利用openpyxl读写Excel文件的方法实例
Feb 03 Python
python模块内置属性概念及实例
Feb 18 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
windows下配置apache+php+mysql时出现问题的处理方法
2014/06/20 PHP
PHP 输出缓冲控制(Output Control)详解
2016/08/25 PHP
thinkPHP订单数字提醒功能的实现方法
2016/12/01 PHP
PHP pthreads v3下同步处理synchronized用法示例
2020/02/21 PHP
js中的时间转换—毫秒转换成日期时间的示例代码
2014/01/26 Javascript
详解JavaScript函数
2015/12/01 Javascript
javascript特效实现——当前时间和倒计时效果的简单实例
2016/07/20 Javascript
js使用原型对象(prototype)需要注意的地方
2017/08/28 Javascript
微信小程序 input表单与redio及下拉列表的使用实例
2017/09/20 Javascript
解决Layui选择全部,换页checkbox复选框重新勾选的问题方法
2018/08/14 Javascript
vue微信分享的实现(在当前页面分享其他页面)
2019/04/16 Javascript
VUE单页面切换动画代码(全网最好的切换效果)
2019/10/31 Javascript
使用Vue Composition API写出清晰、可扩展的表单实现
2020/06/10 Javascript
vue cli 3.0通用打包配置代码,不分一二级目录
2020/09/02 Javascript
Python制作爬虫抓取美女图
2016/01/20 Python
如何利用Fabric自动化你的任务
2016/10/20 Python
Python实现获取照片拍摄日期并重命名的方法
2017/09/30 Python
Python判断一个list中是否包含另一个list全部元素的方法分析
2018/12/24 Python
使用python进行广告点击率的预测的实现
2019/07/04 Python
Django基础知识 web框架的本质详解
2019/07/18 Python
python3常用的数据清洗方法(小结)
2019/10/31 Python
Python Django2.0集成Celery4.1教程
2019/11/19 Python
python实现对列表中的元素进行倒序打印
2019/11/23 Python
Python调用scp向服务器上传文件示例
2019/12/22 Python
Ranorex通过Python将报告发送到邮箱的方法
2020/01/12 Python
python修改linux中文件(文件夹)的权限属性操作
2020/03/05 Python
纯CSS打造(无图像无js)的非常流行的讲话(语音)气泡效果
2012/12/28 HTML / CSS
CSS3实现王者匹配时的粒子动画效果
2019/04/12 HTML / CSS
美国时尚在线:Showpo
2017/09/08 全球购物
合作协议书范文
2014/08/20 职场文书
停电放假通知
2015/04/14 职场文书
监理中标通知书
2015/04/16 职场文书
毕业生捐书活动倡议书
2015/04/27 职场文书
有关三国演义的读书笔记
2015/06/25 职场文书
教务处教学工作总结
2015/08/10 职场文书
如何使用Python提取Chrome浏览器保存的密码
2021/06/09 Python