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之有点简约的元组
Sep 24 Python
在服务器端实现无间断部署Python应用的教程
Apr 16 Python
python中__slots__用法实例
Jun 04 Python
Python制作数据导入导出工具
Jul 31 Python
详解Python中的__new__、__init__、__call__三个特殊方法
Jun 02 Python
python编程使用协程并发的优缺点
Sep 20 Python
使用python 写一个静态服务(实战)
Jun 28 Python
对Tensorflow中Device实例的生成和管理详解
Feb 04 Python
Python实现子类调用父类的初始化实例
Mar 12 Python
Python3 搭建Qt5 环境的方法示例
Jul 16 Python
Python如何进行时间处理
Aug 06 Python
详解python3类型注释annotations实用案例
Jan 20 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
三国漫画《火凤燎原》宣布动画化PV放出 预计2020年播出
2020/03/08 国漫
PHP查询快递信息的方法
2015/03/07 PHP
php操作MongoDB类实例
2015/06/17 PHP
如何使用PHP给图片加水印
2016/10/12 PHP
用JavaScrpt实现文件夹简单轻松加密的实现方法图文
2008/09/08 Javascript
JS 无法通过W3C验证的处理方法
2010/03/09 Javascript
JS this作用域以及GET传输值过长的问题解决方法
2013/08/06 Javascript
详解Javacript和AngularJS中的Promises
2016/02/09 Javascript
Angularjs实现mvvm式的选项卡示例代码
2016/09/08 Javascript
easyui取消表单实时验证,提交时统一验证的简单实例
2016/11/07 Javascript
bootstrap+jQuery 实现下拉菜单中复选框全选和全不选效果
2017/06/12 jQuery
AngularJS与BootStrap模仿百度分页的示例代码
2018/05/23 Javascript
解决vue-cli项目webpack打包后iconfont文件路径的问题
2018/09/01 Javascript
vue element-ui table组件动态生成表头和数据并修改单元格格式 父子组件通信
2019/08/15 Javascript
python实现简单的socket server实例
2015/04/29 Python
python 机器学习之支持向量机非线性回归SVR模型
2019/06/26 Python
Python入门Anaconda和Pycharm的安装和配置详解
2019/07/16 Python
Python logging模块写入中文出现乱码
2020/05/21 Python
Python如何绘制日历图和热力图
2020/08/07 Python
Python实现迪杰斯特拉算法过程解析
2020/09/18 Python
详解如何在登录过期后跳出Ifram框架
2020/09/10 HTML / CSS
韩国三星旗下的一家超市连锁店:Home Plus
2016/07/30 全球购物
手工制作的男士奢华英国鞋和服装之家:Goodwin Smith
2019/06/21 全球购物
值传递还是引用传递
2015/02/08 面试题
师范生个人推荐信
2013/11/29 职场文书
单位实习证明怎么写
2014/01/17 职场文书
爱我中华教学反思
2014/04/28 职场文书
煤矿安全知识竞赛活动总结
2014/07/07 职场文书
党支部创先争优活动总结
2014/08/28 职场文书
工伤事故赔偿协议书范文
2014/09/24 职场文书
开幕式邀请函
2015/01/31 职场文书
绵山导游词
2015/02/05 职场文书
2015年清明节扫墓演讲稿
2015/03/18 职场文书
2019年让高校“心动”的自荐信
2019/03/25 职场文书
php+laravel 扫码二维码签到功能
2021/05/15 PHP
Windows server 2012搭建FTP服务器
2022/04/29 Servers