python中如何统计文本中的单词个数_python统计文本文件内单词数量的方法
本文实例讲述了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程序设计有所帮助。
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:php中文网
python中如何统计文本中的单词个数_python统计文本文件内单词数量的方法相关推荐
- python统计一句话单词个数_python 统计单词个数
标签: 根据一篇英文文章统计其中单词出现最多的10个单词. # -*- coding: utf-8 -*- import urllib2 import re from collections impo ...
- python单词个数_python 统计单词个数
根据一篇英文文章统计其中单词出现最多的10个单词. # -*- coding: utf-8 -*- import urllib2 import re from collections import C ...
- python统计奇数和偶数的个数_Python 统计位数为偶数的数字的代码
问题描述 给出一个整数数组 nums,请返回其中位数为偶数的数字的个数. 示例 1: 输入:nums = [12,345,2,6,7896] 输出:2 解释: 12 是 2 位数字(位数为偶数) 34 ...
- python统计奇数和偶数的个数_Python 统计位数为偶数的数字代码详解
问题描述 给出一个整数数组 nums,请返回其中位数为偶数的数字的个数. 示例 1: 输入:nums = [12,345,2,6,7896] 输出:2 解释: 12 是 2 位数字(位数为偶数) 34 ...
- python统计单词出现次数最多的5个单词_【Python】统计文本中单词的出现次数前十的单词...
代码: # 读取一个文本,并且统计文本中单词的出现次数 def read_file(): # 在windows环境中的编码问题,指定utf-8 with open('F:/python源码/实验区/0 ...
- python中英文字频率_python实现统计文本中单词出现的频率详解
本文实例为大家分享了python统计文本中单词出现频率的具体代码,供大家参考,具体内容如下 #coding=utf-8 import os from collections import Counte ...
- python单词个数统计_Python 统计文本中单词的个数
1.读文件,通过正则匹配 def statisticWord(): line_number = 0 words_dict = {} with open (r'D:\test\test.txt',enc ...
- Python实现统计文本中各单词数量
Python实现统计文本中各单词数量 代码 运行结果 代码 import strings = 'not talk,not helo show me your code hello helo' for ...
- python统计文件中每个单词出现的次数_python统计文本中每个单词出现的次数
.python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...
最新文章
- 第一个spring程序
- 为Delphi程序添加事件和事件处理器
- 最全总结:把模块当做脚本来执行的 7 种案例及其原理
- ixf文件 mysql导入,控制 PC/IXF 文件导入数据库的一般规则
- 一位铁粉的分享:阿里面试归来总结面经,已成功斩获P6 offer...
- WebView点击加载的页面中的按钮时不弹出新窗口以及在加载后执行javascript
- 闭包,sync使用细节
- 全面讲解OpenStack技术知识
- thinkphp连接oracle
- C#LeetCode刷题-Map
- Flink 有状态计算的状态容错
- NodeJs本地搭建服务器,模拟接口请求,获取json数据
- Windows下的Crontab表达式解析DLL的使用
- 用nohup重定向python输出到文件不成功的解决办法
- bzoj 2257: [Jsoi2009]瓶子和燃料(裴蜀定理)
- 邮箱 / QQ 超链接
- 与孩子一起学编程--Ubuntu环境下
- 未来不迎,当时不杂,过往不乱
- wow私服,arcemu trunk源码编译架设
- windows11桌面删除ie图标
热门文章
- InstallShield 2013 应用笔记(一)
- 选择私人区块链技术:Hyperledger Composer
- TAGNN: Target Attentive Graph Neural Networks for Session-based Recommendation论文阅读笔记
- qt QCustomPlot学习
- HDU 4183 Pahom on Water(点双连通分量)
- Autojs连接电脑
- ubuntu16.04 安装openpose
- 培训一周心得体会之如何提高代码质量
- linux v4l2 pdf,linux V4L2编程
- React Native 环境搭建