布同 統(tǒng)計英文單詞的個數的python代碼
更新時間:2011年03月13日 13:21:09 作者:
最近需要翻譯英文文章,所以需要統(tǒng)計單詞個數。索性寫了一段代碼在此,可以簡單的統(tǒng)計單詞的個數
word中對于英文單詞的統(tǒng)計也很好,大家不妨試試。如果沒有安裝word,而且你也是程序員的話,那么可以使用我的這段代碼。通過測試,word的統(tǒng)計結果是18674,軟件的統(tǒng)計結果是18349,相差不到2%,可以作為一個參考。
代碼如下:
# -*- coding: utf-8 -*-
import os,sys
info = os.getcwd() #獲取當前文件名稱
fin = open(u'谷歌C++編程代碼規(guī)范.txt')
info = fin.read()
alist = info.split(' ') # 將文章按照空格劃分開
fout = open(u'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()
代碼如下:
復制代碼 代碼如下:
# -*- coding: utf-8 -*-
import os,sys
info = os.getcwd() #獲取當前文件名稱
fin = open(u'谷歌C++編程代碼規(guī)范.txt')
info = fin.read()
alist = info.split(' ') # 將文章按照空格劃分開
fout = open(u'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中 循環(huán)器 itertools的介紹
循環(huán)器是對象的容器,包含有多個對象。通過調用循環(huán)器的next()方法 (__next__()方法,在Python 3.x中),循環(huán)器將依次返回一個對象。直到所有的對象遍歷窮盡,循環(huán)器將舉出StopIteration錯誤。這篇文章將對此做一個詳細介紹,感興趣的小伙伴請參考下面文字內容2021-09-09python opencv鼠標事件實現畫框圈定目標獲取坐標信息
這篇文章主要為大家詳細介紹了python opencv鼠標事件實現畫框圈定目標,獲取坐標信息,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08Python threading模塊condition原理及運行流程詳解
這篇文章主要介紹了Python threading模塊condition原理及運行流程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10