统计英文单词的个数的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()


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值