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. Ex: If the input is: input1.csv and the contents of input1.csv are: hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy the output is: hello 1 cat 2 man 2 hey 2 dog 2 boy 2 Hello 1 woman 1 Cat 1 Note: There is a newline at the end of the output, and input1.csv is available to download. use "import csv" in code. **This code prints the output in a straight line aross and not down. import csv file = input() frequency = {} with open(file, 'r') as csvfile: csvfile = csv.reader(csvfile) for row in csvfile: for word in row: if word not in frequency.keys(): frequency[word] = 1 else: frequency[word] = frequency[word] + 1 for i in frequency.keys(): print(i,"",frequency[i],end="\t\t")
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.
Ex: If the input is:
input1.csv
and the contents of input1.csv are:
hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy
the output is:
hello 1
cat 2
man 2
hey 2
dog 2
boy 2
Hello 1
woman 1
Cat 1
Note: There is a newline at the end of the output, and input1.csv is available to download. use "import csv" in code.
**This code prints the output in a straight line aross and not down.
import csv
file = input()
frequency = {}
with open(file, 'r') as csvfile:
csvfile = csv.reader(csvfile)
for row in csvfile:
for word in row:
if word not in frequency.keys():
frequency[word] = 1
else:
frequency[word] = frequency[word] + 1
for i in frequency.keys():
print(i,"",frequency[i],end="\t\t")
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images