Python 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 my code is not working, this is what i have: import csv # to store words words= [] # openfile with open('input1.csv') as f: for row in csv.reader(f, delimiter=','): # read data, add to words words += row # get unique words unique_words = sorted(set(words), key=words.index) for word in unique_words : # print words followed by space then its count print(word , words.count(word), end =" ") # formatted output at end print("")
Python
Write a
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
my code is not working, this is what i have:
import csv
# to store words
words= []
# openfile
with open('input1.csv') as f:
for row in csv.reader(f, delimiter=','):
# read data, add to words
words += row
# get unique words
unique_words = sorted(set(words), key=words.index)
for word in unique_words :
# print words followed by space then its count
print(word , words.count(word), end =" ")
# formatted output at end
print("")
Trending now
This is a popular solution!
Step by step
Solved in 2 steps