最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年05月30日 12:29:49   作者:不吃皮蛋  
這篇文章主要介紹了python統計文本文件內單詞數量的方法,涉及Python針對文本文件及字符串的相關操作技巧,需要的朋友可以參考下

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

# count lines, sentences, and words of a text file
# set all the counters to zero
lines, blanklines, sentences, words = 0, 0, 0, 0
print '-' * 50
try:
 # 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 time
for 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 '-' * 50
print "Lines   : ", lines
print "Blank lines: ", blanklines
print "Sentences : ", sentences
print "Words   : ", words
# optional console wait for keypress
from msvcrt import getch
getch()

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

相關文章

最新評論

娄烦县| 西和县| 上饶市| 新余市| 大理市| 石狮市| 临江市| 阜平县| 缙云县| 辰溪县| 丹棱县| 华亭县| 高陵县| 山丹县| 昭觉县| 饶河县| 浮山县| 汉沽区| 江油市| 阳新县| 泰和县| 从江县| 宁城县| 贺州市| 柘城县| 沭阳县| 久治县| 宜黄县| 南郑县| 大名县| 丰县| 江永县| 伊宁市| 阳新县| 山阳县| 加查县| 深泽县| 龙口市| 定南县| 沅江市| 庆城县|