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

到此这篇关于Python实现统计英文单词个数及字符串分割代码就介绍到这了。没有必要去为过去而后悔。因为当下所经历的事情,才最具有现实意义。更多相关Python实现统计英文单词个数及字符串分割代码内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战