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实现控制台进度条功能
Jan 04 Python
Python中你应该知道的一些内置函数
Mar 31 Python
python snownlp情感分析简易demo(分享)
Jun 04 Python
Python读取本地文件并解析网页元素的方法
May 21 Python
python查看模块,对象的函数方法
Oct 16 Python
Python一个简单的通信程序(客户端 服务器)
Mar 06 Python
ipython和python区别详解
Jun 26 Python
Django Aggregation聚合使用方法解析
Aug 01 Python
浅析Python语言自带的数据结构有哪些
Aug 27 Python
简单的命令查看安装的python版本号
Aug 28 Python
python实现单机五子棋
Aug 28 Python
利用python调用摄像头的实例分析
Jun 07 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配置文件中最常用四个ini函数
2007/03/19 PHP
基于simple_html_dom的使用小结
2013/07/01 PHP
PHP如何将XML转成数组
2016/04/04 PHP
CentOS 上搭建 PHP7 开发测试环境
2017/02/26 PHP
PHP数组去重的更快实现方式分析
2018/05/09 PHP
php实现微信支付之现金红包
2018/05/30 PHP
php实现简单四则运算器
2020/11/29 PHP
JavaScript 事件对象的实现
2009/07/13 Javascript
Js四则运算函数代码
2012/07/21 Javascript
《JavaScript函数式编程》读后感
2015/08/07 Javascript
jQuery Real Person验证码插件防止表单自动提交
2015/11/06 Javascript
angularjs自定义ng-model标签的属性
2016/01/21 Javascript
javascript创建cookie、读取cookie
2016/03/31 Javascript
Angualrjs 表单验证的两种方式(失去焦点验证和点击提交验证)
2017/05/09 Javascript
基于Vuex无法观察到值变化的解决方法
2018/03/01 Javascript
vue 父组件调用子组件方法及事件
2018/03/29 Javascript
微信小程序 组件的外部样式externalClasses使用详解
2019/09/06 Javascript
JavaScript 生成唯一ID的几种方式
2021/02/19 Javascript
Python抓取框架Scrapy爬虫入门:页面提取
2017/12/01 Python
python截取两个单词之间的内容方法
2018/12/25 Python
OpenCV+Python识别车牌和字符分割的实现
2019/01/31 Python
简单了解Python生成器是什么
2019/07/02 Python
django有哪些好处和优点
2020/09/01 Python
pytorch 实现L2和L1正则化regularization的操作
2021/03/03 Python
利用CSS的Sass预处理器(框架)来制作居中效果
2016/03/10 HTML / CSS
拉飞逸官网:Lafayette 148 New York
2020/07/15 全球购物
个人作风剖析材料
2014/02/02 职场文书
学习标兵获奖感言
2014/02/20 职场文书
企业领导班子四风对照检查材料
2014/09/27 职场文书
见习报告怎么写
2014/10/31 职场文书
2016猴年春节慰问信
2015/11/30 职场文书
2019年预备党员的思想汇报:加深对党的认知
2019/09/25 职场文书
django注册用邮箱发送验证码的实现
2021/04/18 Python
Python数据分析入门之数据读取与存储
2021/05/13 Python
使用CSS连接数据库的方式
2022/02/28 HTML / CSS
阿里云国际版 使用Nginx作为HTTPS转发代理服务器
2022/05/11 Servers