split函数python统计英文单词_Python实现统计英文单词个数及字符串分割代码
Python實(shí)現(xiàn)統(tǒng)計(jì)英文單詞個(gè)數(shù)及字符串分割代碼
這篇文章主要介紹了Python實(shí)現(xiàn)統(tǒng)計(jì)英文單詞個(gè)數(shù)及字符串分割方法,本文分別給出代碼實(shí)例,需要的朋友可以參考下
字符串分割
代碼如下:
str="a|and|hello|||ab"
alist = str.split('|')
print alist
結(jié)果
復(fù)制代碼代碼如下:
str="a hello{這里換成5個(gè)空格}world{這里換成3個(gè)空格}"
alist=str.split(' ')
print alist
統(tǒng)計(jì)英文單詞的個(gè)數(shù)的python代碼
代碼如下:
# -*- coding: utf-8 -*-
import os,sys
info = os.getcwd() #獲取當(dāng)前文件名稱
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) # 總的單詞數(shù)
nulen = alist.count('') # 空格的數(shù)量
print "words' number is",allen
print "null number is",nulen
print "poor words number is", allen-nulen # 實(shí)際的單詞數(shù)目
fin.close()
完 謝謝觀看
總結(jié)
以上是生活随笔為你收集整理的split函数python统计英文单词_Python实现统计英文单词个数及字符串分割代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 教学计划计算机,有关计算机教学计划三篇
- 下一篇: python帮你获取王者荣耀金币