字符串分割
str="a|and|hello|||ab"
alist = str.split('|')
print alist
結(jié)果
str="a hello{這里換成5個空格}world{這里換成3個空格}"
alist=str.split(' ')
print alist
統(tǒng)計英文單詞的個數(shù)的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) # 總的單詞數(shù)
nulen = alist.count('') # 空格的數(shù)量
print "words' number is",allen
print "null number is",nulen
print "poor words number is", allen-nulen # 實際的單詞數(shù)目
fin.close()