PYTHON programming, please! Write a program that counts how often each word occu
ID: 3810362 • Letter: P
Question
PYTHON programming, please!
Write a program that counts how often each word occurs in the supplied text file mary.txt. Your program should output one line for each unique word that occurs in the text file in the format word: count, where word is a word from the file and count is the number of times that word occurs. Be sure to strip punctuation from words or your program will won't produce the correct output.
(Hint : The dictionary data structure should be used for this problem)
mary.txt is the following
Explanation / Answer
def wordCount(fileName):
d = {}
for line in open(fileName):
words = line.split()
for word in words:
word = word.lstrip('?:!.,;"')
word = word.rstrip('?:!.,;"')
word = word.lower()
if word not in d:
d[word] = 1
else:
d[word] = d[word] + 1
for i in d:
print i + ' : ' + str(d[i])
wordCount('input.txt')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.