1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
a_sh-v [17]
3 years ago
5

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 co

ntains 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.

Computers and Technology
1 answer:
DaniilM [7]3 years ago
3 0

Answer:

Here is the Python code.

import csv  

inputfile = input("Enter name of the file: ")

count = {}

with open(inputfile, 'r') as csvfile:

 csvfile = csv.reader(csvfile)

 for line in csvfile:

   for words in line:

     if words not in count.keys():

       count[words] = 1

     else:

         count[words] + 1

   print(count)

Explanation:

I will explain the code line by line.

import csv   here the css is imported in order to read the file using csv.reader() method

inputfile = input("Enter name of the file: ")  This statement takes the name of the input file from the user

count = {}  this is basically a dictionary which will store the words in the file and the number of times each word appears in the file.

with open(inputfile, 'r') as csvfile:  This statement opens the inputfile using open() method in read mode. This basically opens the input file as csv type

 csvfile = csv.reader(csvfile)  This statement uses csv.reader() method to read the csv file contents.

for line in csvfile:  This outer for loop iterates through each line of the file

for words in line:  This inner loop iterates through each word in the line

if words not in count.keys():  This if condition checks if the word is not in the dictionary already. The dictionary holds key value pair and keys() method returns a list of all the keys of dictionary

count[words] = 1  if the word is not present already then assign 1 to the count dictionary

co unt[words] + 1 if the word is already present in dictionary increment count of the words by 1. Suppose the input file contains the following words:

hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy

Then because of co unt[words] + 1 statement if the word appears more than once in the file, then it will not be counted. So the output will be:

{' cat': 1, ' man': 1, ' dog': 1, ' woman': 1, ' Hello': 1, ' hey': 1, ' boy': 1, 'hello': 1, ' Cat': 1}

But if you want the program to count those words also in the file that appear more than once, then change this co unt[words] + 1 statement to:  

count[words] = count[words] + 1

So if the word is already present in the file its frequency is increased by 1 by incrementing 1 to the count[words]. This will produce the following output:

{' Cat': 1, ' cat': 2, ' boy': 2, ' woman': 1, ' dog': 2, ' hey': 2, ' man': 2, ' Hello': 1, 'hello': 1}  

You can see that cat, boy, dog and hey appear twice in the file.                    

print(count) This statement prints the dictionary contents with words and their frequencies.

The program with its output is attached.

You might be interested in
A is a way for students to keep track of information during research
Ilya [14]
The choices are a) credible website B)search log c) Boolean operator d) online database.

The answer is, d. Online database. There are already numerous online digital tools that can aid research. Examples are readcube, mendeley, google scholar. It helps you save and organize researches as well as automatically give bibliographies according to your need like APA and MLA. These tools can hold and save different information that researchers need.
6 0
3 years ago
What is the informal language that programmers use to create models of programs that have no syntax rules and are not meant to b
Yuri [45]
Pcode (Pseudo-code).

-----------------------------
5 0
3 years ago
when you select cells in your worksheet and then click the copy button, what happens? select one: a. the formatting is removed f
Amiraneli [1.4K]
Hey there! Hello!

Not sure if you still need this answer, but I'd love to help out if you do.

In terms if Excel Spreadsheet, your answer will be D. A<span> moving dashed line border appears around the cells. What the copy button does is take the contents in your cell(s) and copies them so that you can paste them elsewhere. B and C would apply here if you were cutting the text, which clears whatever you have selected so that you can paste them elsewhere without having to go back and delete it. 

I have attached an example of what happens to a cell when you either right click it and press copy, or press ctrl/command+c, which is the keyboard shortcut for copying text or contents. 

Hope this helped you out! Feel free to ask me any additional questions if you have any. :-)</span>

4 0
3 years ago
Availability is an essential part of ________ security, and user behavior analysis and application analysis provide the data nee
sertanlavr [38]

Answer:

Network

Availability is an essential part of Network security, and user behavior analysis and application analysis provide the data needed to ensure that systems are available.

7 0
3 years ago
Can I buy this website?
Serggg [28]

Answer:

I the domain already purchased?

Explanation:

7 0
3 years ago
Other questions:
  • Beth’s multimedia business specializes in sound integration and editing. A New project requires video editing and the creation o
    7·1 answer
  • Of the sequences listed below, which shows the correct order of the steps in the incident management workflow: (1) authenticate
    14·1 answer
  • Please help quickly!!! which of the following is not a peripheral?
    8·2 answers
  • Write a statement that compares the values of score1 and score2 and takes the following actions. When score1 exceeds score2, the
    7·1 answer
  • Which procedure is used as a physical barrier to secure data against unauthorized access in a cloud storage data center?
    6·1 answer
  • Select the correct answer. Vlad wants to include his goals and target in his résumé. He also wants to add how he can be benefici
    12·1 answer
  • Gimme Shelter Roofers maintains a file of past customers, including a customer number, name, address, date of job, and price of
    8·1 answer
  • A data center needs to ensure that data is not lost at the system level in the event of a blackout. Servers must stay operable f
    6·1 answer
  • HELP ASAP DONT ANSWER WITH A LINK​
    9·1 answer
  • What safety do you need to have ready to start a device repair?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!