您好,欢迎来到飒榕旅游知识分享网。
搜索
您的当前位置:首页Python实现统计英文单词个数及字符串分割代码

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

Copyright © 2019- sarr.cn 版权所有 赣ICP备2024042794号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务