NEED HELP-process words in a text file

Dave Angel davea at ieee.org
Sat Jun 18 22:05:55 EDT 2011


On 01/-10/-28163 02:59 PM, Cathy James wrote:
> Dear Python Experts,
>
> First, I'd like to convey my appreciation to you all for your support
> and contributions.  I am a Python newborn and need help with my
> function. I commented on my program as to what it should do, but
> nothing is printing. I know I am off, but not sure where. Please
> help:(
>
> import string
> def fileProcess(filename):
>      """Call the program with an argument,
>      it should treat the argument as a filename,
>      splitting it up into words, and computes the length of each word.
>      print a table showing the word count for each of the word lengths
> that has been encountered.
>      Example:
>      Length Count
>      1 16
>      2 267
>      3 267
>      4 169
>      >>>"&"
>      Length    Count
>      0    0
>      >>>
>      >>>"right."
>      Length    Count
>      5    10
>      """
>      freq = [] #empty dict to accumulate words and word length
>      filename=open('declaration.txt, r')
>      for line in filename:
>          punc = string.punctuation + string.whitespace#use Python's
> built-in punctuation and whiitespace
>          for i, word in enumerate (line.replace (punc, "").lower().split()):
>              if word in freq:
>                  freq[word] +=1 #increment current count if word already in dict
>
>              else:
>                  freq[word] = 0 #if punctuation encountered,
> frequency=0 word length = 0
>          for word in freq.items():
>              print("Length /t"+"Count/n"+ freq[word],+'/t' +
> len(word))#print word count and length of word separated by a tab
>
>
>
>
>      #Thanks in advance,
> CJ.
>

You create a function fileProcess() with several bugs in it.  But you 
never call it, so the bugs are irrelevant.

The script does precisely nothing.

DaveA




More information about the Python-list mailing list