<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: open code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 11 Oct 2008 04:11:28 GMT</pubDate>
    <description>DZone Snippets: open code</description>
    <item>
      <title>count lines, sentences, and words of a text file</title>
      <link>http://snippets.dzone.com/posts/show/503</link>
      <description>&lt;code&gt;&lt;br /&gt;# count lines, sentences, and words of a text file&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# set all the counters to zero&lt;br /&gt;&lt;br /&gt;lines, blanklines, sentences, words = 0, 0, 0, 0&lt;br /&gt;&lt;br /&gt;print '-' * 50&lt;br /&gt;&lt;br /&gt;try:&lt;br /&gt;  # use a text file you have, or google for this one ...&lt;br /&gt;&lt;br /&gt;  filename = 'GettysburgAddress.txt'&lt;br /&gt;  textf = open(filename, 'r')&lt;br /&gt;except IOError:&lt;br /&gt;  print 'Cannot open file %s for reading' % filename&lt;br /&gt;  import sys&lt;br /&gt;  sys.exit(0)&lt;br /&gt;&lt;br /&gt;# reads one line at a time&lt;br /&gt;&lt;br /&gt;for line in textf:&lt;br /&gt;  print line,   # test&lt;br /&gt;&lt;br /&gt;  lines += 1&lt;br /&gt;  &lt;br /&gt;  if line.startswith('\n'):&lt;br /&gt;    blanklines += 1&lt;br /&gt;  else:&lt;br /&gt;    # assume that each sentence ends with . or ! or ?&lt;br /&gt;&lt;br /&gt;    # so simply count these characters&lt;br /&gt;&lt;br /&gt;    sentences += line.count('.') + line.count('!') + line.count('?')&lt;br /&gt;    &lt;br /&gt;    # create a list of words&lt;br /&gt;&lt;br /&gt;    # use None to split at any whitespace regardless of length&lt;br /&gt;&lt;br /&gt;    # so for instance double space counts as one space&lt;br /&gt;&lt;br /&gt;    tempwords = line.split(None)&lt;br /&gt;    print tempwords  # test&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;    # word total count&lt;br /&gt;&lt;br /&gt;    words += len(tempwords)&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;textf.close()&lt;br /&gt;&lt;br /&gt;print '-' * 50&lt;br /&gt;print "Lines      : ", lines&lt;br /&gt;print "Blank lines: ", blanklines&lt;br /&gt;print "Sentences  : ", sentences&lt;br /&gt;print "Words      : ", words&lt;br /&gt;&lt;br /&gt;# optional console wait for keypress&lt;br /&gt;&lt;br /&gt;from msvcrt import getch&lt;br /&gt;getch()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 24 Jul 2005 11:45:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/503</guid>
      <author>mattisbusy (Matt Kaufman)</author>
    </item>
  </channel>
</rss>
