Write a python program that reads the contents of a text file. The program shoul
ID: 3538613 • Letter: W
Question
Write a python program that reads the contents of a text file. The program should create a dictionary in which the keys are the individual words found in the file and the values are the number of times each word appears.For example, if the word "the" appears 128 times, the dictionary would contain an element with 'the' as the key and 128 as the value. The program should either display the frequency of each word or create a seocnd file containing a list of each word and its frequency.
This is the text I was planning to test it on: http://pastebin.com/RiaWtanQ
Thanks in advance.
Explanation / Answer
#!/usr/local/bin/python3
file = open("input.txt")
dictornary={}
while 1:
line = file.readline()
kt=line.split(' ');
if not line:
break
for i in kt:
try:
dictornary[i]=dictornary[i]+1
except KeyError:
dictornary[i]=1
for key in dictornary.keys():
print (key , dictornary[key])
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.