首页 > 编程语言 > 详细

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

时间:2016-02-08 23:00:40      阅读:360      评论:0      收藏:0      [点我收藏+]

字符串分割

复制代码 代码如下:

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()

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

原文:http://www.jb51.net/article/66848.htm

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!