本文實例講述了Python計算一個文件里字?jǐn)?shù)的方法。分享給大家供大家參考。具體如下:
這段程序從所給文件中找出字?jǐn)?shù)來。
from string import *def countWords(s): words=split(s) return len(words) #returns the number of wordsfilename=open("welcome.txt",'r')#open an file in reading modetotal_words=0for line in filename: total_words=total_words + countWords(line) #counts the total wordsprint total_words
希望本文所述對大家的Python程序設(shè)計有所幫助。
新聞熱點
疑難解答
圖片精選