• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python统计文本文件内单词数量的方法

python统计文本文件内单词数量的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python文本编辑器,python文本分类,python中文文本分类,python 读取文本,python中文文本挖掘等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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()

</div>

希望本文所述对大家的python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python简单检测文本类型的2种方法【基于文件头及cchardet库】
  • Python简单检测文本类型的2种方法【基于文件头及cchardet库】
  • Python如何实现文本转语音
  • Python读写txt文本文件的操作方法全解析
  • Python的Flask站点中集成xhEditor文本编辑器的教程
  • Python的Flask站点中集成xhEditor文本编辑器的教程
  • Python判断文本中消息重复次数的方法
  • Python判断文本中消息重复次数的方法
  • 使用Python读写文本文件及编写简单的文本编辑器
  • 使用Python读写文本文件及编写简单的文本编辑器

相关文章

  • Python函数式编程指南(四):生成器详解
  • python生成器表达式和列表解析
  • python中二维阵列的变换实例
  • 在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程
  • Python牛刀小试密码爆破
  • python k-近邻算法实例分享
  • 简单介绍Python中的readline()方法的使用
  • python集合类型用法分析
  • python中global用法实例分析
  • python实现的一只从百度开始不断搜索的小爬虫

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 编写简单的Python程序来判断文本的语种
    • Python最基本的输入输出详解
    • 详解Python当中的字符串和编码
    • 使用PYTHON接收多播数据的代码
    • Python and、or以及and-or语法总结
    • python实现从字典中删除元素的方法
    • Python实现TCP/IP协议下的端口转发及重定向示例
    • Python实现快速多线程ping的方法
    • Python 登录网站详解及实例
    • 使用PYTHON接收多播数据的代码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有