-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordFrequenciesPython.txt
More file actions
23 lines (18 loc) · 1.82 KB
/
Copy pathWordFrequenciesPython.txt
File metadata and controls
23 lines (18 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Assignment: Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method. The file contains a list of words separated by commas. Your program should output the words and their frequencies (the number of times each word appears in the file) without any duplicates.
import csv
import_file = input()
# open the file
with open(import_file, 'r') as csvfile:
contents = csv.reader(csvfile, delimiter=',')
# create an empty dictionary
word_list = {}
# see if a word already exists in the dictionary, if yes, add one
for word in contents:
for word_count in word:
if word_count in word_list:
word_list[word_count] += 1
else:
word_list[word_count] = 1
# print the list and the frequency of the word
for w in word_list:
print('{} {}'.format(w, word_list[w]))