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通过定义一个类实例作为ftp回调方法
May 04 Python
简单了解Python中的几种函数
Nov 03 Python
python 寻找list中最大元素对应的索引方法
Jun 28 Python
python实时获取外部程序输出结果的方法
Jan 12 Python
详解Python中的测试工具
Jun 09 Python
Python re 模块findall() 函数返回值展现方式解析
Aug 09 Python
在python image 中安装中文字体的实现方法
Aug 22 Python
Python基于百度AI实现OCR文字识别
Apr 02 Python
Python数据正态性检验实现过程
Apr 18 Python
python实现Oracle查询分组的方法示例
Apr 30 Python
Python实现冒泡排序算法的完整实例
Nov 04 Python
如何利用python实现Simhash算法
Jun 28 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
图形数字验证代码
2006/10/09 PHP
聊天室php&mysql(二)
2006/10/09 PHP
一个用php实现的获取URL信息的类
2007/01/02 PHP
php对mongodb的扩展(初出茅庐)
2012/11/11 PHP
php中的注释、变量、数组、常量、函数应用介绍
2012/11/16 PHP
php建立Ftp连接的方法
2015/03/07 PHP
php线性表的入栈与出栈实例分析
2015/06/12 PHP
WIFI万能钥匙密码查询接口实例
2015/09/28 PHP
(currentStyle)javascript为何有时用style得不到已设定的CSS的属性
2007/08/15 Javascript
extjs 的权限问题 要求控制的对象是 菜单,按钮,URL
2010/03/09 Javascript
textarea中的手动换行处理的jquery代码
2011/02/26 Javascript
使用jQuery中的wrap()函数操作HTML元素的教程
2016/05/24 Javascript
Mvc提交表单的四种方法全程详解
2016/08/10 Javascript
JavaScript 继承详解(五)
2016/10/11 Javascript
JavaScript实现购物车基本功能
2017/07/21 Javascript
Angularjs实现数组随机排序的方法
2018/10/02 Javascript
Vue-CLI 项目在pycharm中配置方法
2019/08/30 Javascript
vue+webpack dev本地调试全局样式引用失效的解决方案
2019/11/12 Javascript
Vue组件模板的几种书写形式(3种)
2020/02/19 Javascript
Electron整合React使用搭建开发环境的步骤详解
2020/06/07 Javascript
Python处理RSS、ATOM模块FEEDPARSER介绍
2015/02/18 Python
python的多重继承的理解
2017/08/06 Python
如何在python中使用selenium的示例
2017/12/26 Python
Python单元测试简单示例
2018/07/03 Python
opencv python 2D直方图的示例代码
2018/07/20 Python
Python判断两个文件是否相同与两个文本进行相同项筛选的方法
2019/03/01 Python
Python笔记之工厂模式
2019/11/20 Python
Python环境使用OpenCV检测人脸实现教程
2020/10/19 Python
HTML5操作WebSQL数据库的实例代码
2017/08/26 HTML / CSS
旷课检讨书2000字
2014/01/14 职场文书
内勤主管岗位职责
2014/04/03 职场文书
护理专业求职信
2014/06/15 职场文书
给男朋友的道歉短信
2015/05/12 职场文书
python之django路由和视图案例教程
2021/07/26 Python
「SHOW BY ROCK!!」“雫シークレットマインド”组合单曲MV公开
2022/03/21 日漫
Win11查看设备管理器
2022/04/19 数码科技