国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python統計文本文件內單詞數量的方法

2019-11-25 17:20:55
字體:
來源:轉載
供稿:網友

本文實例講述了python統計文本文件內單詞數量的方法。分享給大家供大家參考。具體實現方法如下:

# count lines, sentences, and words of a text file# set all the counters to zerolines, blanklines, sentences, words = 0, 0, 0, 0print '-' * 50try: # use a text file you have, or google for this one ... filename = 'GettysburgAddress.txt' textf = open(filename, 'r')except IOError: print 'Cannot open file %s for reading' % filename import sys sys.exit(0)# reads one line at a timefor line in textf: print line,  # test lines += 1 if line.startswith('/n'):  blanklines += 1 else:  # assume that each sentence ends with . or ! or ?  # so simply count these characters  sentences += line.count('.') + line.count('!') + line.count('?')  # create a list of words  # use None to split at any whitespace regardless of length  # so for instance double space counts as one space  tempwords = line.split(None)  print tempwords # test  # word total count  words += len(tempwords)textf.close()print '-' * 50print "Lines   : ", linesprint "Blank lines: ", blanklinesprint "Sentences : ", sentencesprint "Words   : ", words# optional console wait for keypressfrom msvcrt import getchgetch()

希望本文所述對大家的python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 图片| 山丹县| 大田县| 栖霞市| 金塔县| 云霄县| 凭祥市| 新巴尔虎左旗| 图们市| 怀集县| 龙泉市| 西青区| 芮城县| 炎陵县| 和平区| 皮山县| 平昌县| 中山市| 盖州市| 黄龙县| 平陆县| 伊宁市| 隆尧县| 武山县| 封开县| 祁门县| 沈阳市| 禄劝| 邯郸县| 仁寿县| 蒲江县| 吉首市| 焉耆| 奇台县| 宜章县| 宝应县| 南通市| 灌阳县| 荃湾区| 左权县| 嵊泗县|