布同 统计英文单词的个数的python代码

yipeiwu_com5年前Python基础
word中对于英文单词的统计也很好,大家不妨试试。如果没有安装word,而且你也是程序员的话,那么可以使用我的这段代码。通过测试,word的统计结果是18674,软件的统计结果是18349,相差不到2%,可以作为一个参考。

  代码如下:
复制代码 代码如下:

# -*- coding: utf-8 -*-

import os,sys
info = os.getcwd() #获取当前文件名称
fin = open(u'谷歌C++编程代码规范.txt')

info = fin.read()
alist = info.split(' ') # 将文章按照空格划分开

fout = open(u'count.txt', 'w')
fout.write('\n'.join(alist)) # 可以通过文本文件的行号同样看到效果
##fout.write('%s' % alist)
fout.close()

allen = len(alist) # 总的单词数
nulen = alist.count('') # 空格的数量
print "words' number is",allen
print "null number is",nulen
print "poor words number is", allen-nulen # 实际的单词数目

fin.close()

相关文章

两个元祖T1=('a', 'b'),T2=('c', 'd')使用匿名函数将其转变成[{'a': 'c'},{'b': 'd'}]的几种方法

一道Python面试题的几种解答: 两个元祖T1=('a', 'b'), T2=('c', 'd'),请使用匿名函数将其转变成[{'a': 'c'}, {'b': 'd'}] 方法一:...

Django中利用filter与simple_tag为前端自定义函数的实现方法

前言 Django的模板引擎提供了一般性的功能函数,通过前端可以实现多数的代码逻辑功能,这里称之为一般性,是因为它仅支持大多数常见情况下的函数功能,例如if判断,ifequal对比返回值...

让Python脚本暂停执行的几种方法(小结)

1.time.sleep(secs) 参考文档原文: Suspend execution for the given number of seconds. The argument m...

pytorch中torch.max和Tensor.view函数用法详解

torch.max() 1. torch.max()简单来说是返回一个tensor中的最大值。 例如: >>> si=torch.randn(4,5) >&g...

Python编程图形库之Pillow使用方法讲解

Python编程图形库之Pillow使用方法讲解

PIL vs Pillow PIL: Python Imaging Library,是python的图像处理库。由于PIL不兼容setuptools,再加上更新缓慢等因素,Alex Cl...