OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

7.8 LAB: Word frequencies (lists and CSV) - How can I read a csv and eliminate duplicates?

  • Thread starter Thread starter KnowsNothing
  • Start date Start date
K

KnowsNothing

Guest
Here is the code I have but I cannot figure out how to remove the duplicates. I've tried making new lists, dict, tuples, etc.



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:

Code:
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.

Code:
import csv
user_input = input() 
with open(user_input, 'r') as name_CSV: 
    paper_copy = csv.reader(name_CSV)
    for lines in paper_copy:
        for w in lines: 
            words_cnt = lines.count(w)
            print(w, words_cnt)
<p>Here is the code I have but I cannot figure out how to remove the duplicates. I've tried making new lists, dict, tuples, etc.</p>
<hr />
<p>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.</p>
<p>Ex: If the input is:</p>
<p><code>input1.csv</code></p>
<p>and the contents of input1.csv are:</p>
<p><code>hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy</code></p>
<p>the output is:</p>
<pre><code>hello 1
cat 2
man 2
hey 2
dog 2
boy 2
Hello 1
woman 1
Cat 1
</code></pre>
<p>Note: There is a newline at the end of the output, and input1.csv is available to download.</p>
<pre><code>import csv
user_input = input()
with open(user_input, 'r') as name_CSV:
paper_copy = csv.reader(name_CSV)
for lines in paper_copy:
for w in lines:
words_cnt = lines.count(w)
print(w, words_cnt)
</code></pre>
 
Top