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从使用线程到使用async/await的深入讲解
Sep 16 Python
对python 生成拼接xml报文的示例详解
Dec 28 Python
python遍历小写英文字母的方法
Jan 02 Python
Python JSON格式数据的提取和保存的实现
Mar 22 Python
python psutil模块使用方法解析
Aug 01 Python
pytorch 批次遍历数据集打印数据的例子
Dec 30 Python
Ubuntu中配置TensorFlow使用环境的方法
Apr 21 Python
Python使用Selenium实现淘宝抢单的流程分析
Jun 23 Python
python中执行smtplib失败的处理方法
Jul 01 Python
python输入中文的实例方法
Sep 14 Python
Python+Appium实现自动化清理微信僵尸好友的方法
Feb 04 Python
python中的3种定义类方法
Nov 27 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
收音机发烧友应当熟知的100条知识
2021/03/02 无线电
php.ini中的php-5.2.0配置指令详解
2008/03/27 PHP
深入php var_dump()函数的详解
2013/06/05 PHP
3款实用的在线JS代码工具(国外)
2012/03/15 Javascript
如何使用jquery控制CSS样式,并且取消Css样式(如背景色,有实例)
2013/07/09 Javascript
全面解析Bootstrap图片轮播效果
2015/12/03 Javascript
angular route中使用resolve在uglify压缩后问题解决
2016/09/21 Javascript
第一次接触Bootstrap框架
2016/10/24 Javascript
Bootstrap常用组件学习(整理)
2017/03/24 Javascript
详解用webpack把我们的业务模块分开打包的方法
2017/07/20 Javascript
简单的Vue SSR的示例代码
2018/01/12 Javascript
vue中手机号,邮箱正则验证以及60s发送验证码的实例
2018/03/16 Javascript
JavaScript 下载svg图片为png格式
2018/06/21 Javascript
vue自定义指令directive的使用方法
2019/04/07 Javascript
vue 动态生成拓扑图的示例
2021/01/03 Vue.js
[03:59]DOTA2英雄梦之声_第07期_水晶室女
2014/06/23 DOTA
[04:13]2014DOTA2国际邀请赛 专访DC目前形势不容乐观
2014/07/12 DOTA
[01:21]DOTA2新纪元-7.0新版本即将开启!
2016/12/11 DOTA
python对url格式解析的方法
2015/05/13 Python
python中redis的安装和使用
2016/12/04 Python
Python数据分析中Groupby用法之通过字典或Series进行分组的实例
2017/12/08 Python
对python实现合并两个排序链表的方法详解
2019/01/23 Python
python绘制BA无标度网络示例代码
2019/11/21 Python
django框架单表操作之增删改实例分析
2019/12/16 Python
PyTorch的SoftMax交叉熵损失和梯度用法
2020/01/15 Python
Myprotein葡萄牙官方网站:英国优质运动营养品牌
2016/09/12 全球购物
媒体宣传策划方案
2014/05/25 职场文书
自查自纠工作情况报告
2014/10/29 职场文书
酒店工程部岗位职责
2015/02/12 职场文书
导游词格式
2015/02/13 职场文书
2015年医德医风工作总结
2015/04/02 职场文书
求职信如何撰写?
2019/05/22 职场文书
Pytorch中TensorBoard及torchsummary的使用详解
2021/05/12 Python
python​格式化字符串
2022/04/20 Python
openstack云计算keystone组件工作介绍
2022/04/20 Servers
索尼ICF-36收音机评测
2022/04/30 无线电