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

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

概述字符串分割复制代码代码如下:str=\"a|and|hello|||ab\"alist=str.split(\'|\')printalist结果

字符串分割
复制代码 代码如下:
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实现统计英文单词个数及字符串分割代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1202675.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存