NEED HELP-process words in a text file

Cathy James nambo4jb at gmail.com
Sat Jun 18 19:21:55 EDT 2011


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.


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /