Word count from file help.

Paul McGuire ptmcg at users.sourceforge.net
Thu Feb 12 11:23:20 EST 2004


Oops, sorry, forgot to mention that this requires downloading pyparsing at
http://pyparsing.sourceforge.net.

"Paul McGuire" <ptmcg at users.sourceforge.net> wrote in message
news:HENWb.8393$ng6.3610 at fe2.texas.rr.com...
> "jester.dev" <jester.dev at comcast.net> wrote in message
> news:oiAWb.9926$jk2.28236 at attbi_s53...
> > Hello,
> >
> >         I'm learning Python from Python Bible, and having some
> > problems with this code below. When I run it, I get nothing. It
> > should open the file poem.txt (which exists in the current
> > directory) and count number of times any given word appears
> > in the text.
> >
> Try this:
>
> # wordCount.py
> #
> # invoke using:   python wordCount.py  <filename>
> #
> from pyparsing import Word, alphas
> import sys
>
> # modify this word definition as you wish - whitespace is implicit
separator
> wordSpec = Word(alphas)
>
> if len(sys.argv) > 1:
>     infile = sys.argv[1]
>
>     wordDict = {}
>     filetext = "\n".join( file(infile).readlines() )
>     for wd,locstart,locend in wordSpec.scanString(filetext):
>         curWord = wd[0].lower()
>         if wordDict.has_key( curWord ):
>             wordDict[curWord] += 1
>         else:
>             wordDict[curWord] = 1
>
>     print "%s has %d different words." % ( infile, len(wordDict.keys()) )
>     keylist = wordDict.keys()
>     keylist.sort( lambda a,b:
>                     ( wordDict[b] - wordDict[a] ) or
>                     ( ( ( a > b ) and 1 ) or ( ( a < b ) and -1 ) or 0 ) )
>     for k in keylist:
>         print k, ":", wordDict[k]
>
>





More information about the Python-list mailing list