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
True or false.the color attribute cannot recognized the hexadecimal code.
vlada-n [284]

Answer:

true

Explanation:

A hex triplet is a six-digit, three-byte hexadecimal number used in HTML, CSS, SVG, and other computing applications to represent colors.

4 0
2 years ago
Read 2 more answers
Using the lab's show ip sla statistics example, what does the failure count indicate about the web server?
NeX [460]

It shows the number of times SLA ICMP (Internet Control Message Protocol), Protocol (TCP), File Transfer Protocol (FTP), Dynamic Host Configuration Protocol (DHCP), Domain Name System (DNS), or UDP echo operations did not manage to reach the web server.






7 0
3 years ago
Average of Grades - Write a program that stores the following values in five different variables: 98, 87, 84, 100, 94. The progr
liubo4ka [24]

Answer:

Not sure what language, but in python a super basic version would be:

val1 = 98

val2 = 87

val3 = 84

val4 = 100

val5 = 94

sum = val1 + val2 + val3 + val4 + val5

avg = sum / 5

print(avg)

Explanation:

4 0
3 years ago
What is an example of a reputable website
Keith_Richards [23]
A reputable website would be one that you could research online and read about it's history , reviews etc .
4 0
3 years ago
Read 2 more answers
What are two reasons a network administrator would use cdp.
Ulleksa [173]

Answer:

To obtain VLAN information from directly connected switches. To determine the status of network services on a remote device. To determine the status of the routing protocols between directly connected routers .

Explanation:

6 0
2 years ago
Other questions:
  • USB keys can store terabytes of data. Of the five key factors that contribute to the increasing vulnerability of organizational
    13·1 answer
  • What does the clone tile command do?
    15·2 answers
  • What is the "online disinhibition effect"?​
    7·1 answer
  • Write a program that allows two players to play a game of tic-tac-toe. Use a two dimensional char array with three rows and thre
    7·1 answer
  • Which one of these is correct PI of a XML document?
    13·1 answer
  • A good practice when using public domain content is to
    7·1 answer
  • Describe two types of storage devices?​
    15·1 answer
  • What<br>are<br>the features of secondary storage media​
    13·2 answers
  • Write a user input program that simulates a game of a rolling pair of dice. You can create/simulate rolling one die by choosing
    10·1 answer
  • What happened to the ten commandments tablets.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!