微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python实现统计英文单词个数及字符串分割代码

字符串分割


str="a|and|hello|||ab"
alist = str.split('|')
print alist

结果



str="a hello{这里换成5个空格}world{这里换成3个空格}"
alist=str.split(' ')
print alist

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


# -*- coding: utf-8 -*-
import os,sys
info = os.getcwd() #获取当前文件名称
fin = open(u'c:/a.txt')

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

fout = open(u'c:/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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐