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
Which of the following TCP/IP settings should be configured to specify DNS suffixes to use other than resolving names through a
Harman [31]

Answer:

b. Append these DNS suffixes (in order)

Explanation:

The correct TCP/IP settings to configure to specify DNS suffixes is to append these DNS suffixes in order. This option is normally chosen by default. It is usually selected when the aim is to resolve unqualified computer names present in the primary domain. For example, the name of the computer being used is michael and the parent domain name in question is google.com, this would therefore resolve the computer name to michael.google.com.

5 0
3 years ago
A computer that no longer works after having minor repair works done to it may have been damaged by
FinnZ [79.3K]
Shipping or misrepair.
3 0
3 years ago
Read 2 more answers
What is the purpose of the Occupational Safety and Health Act?
NikAS [45]

Answer:

The purpose of this act was to reduce workplace hazards and implement more safety and health programs for both employers and their employees.  

3 0
3 years ago
Read 2 more answers
Chunking is a good strategy for completing large assignments because it makes the work
Bess [88]

Answer:

ExChunking is a good strategy for completing large assignments because it makes the work

less boring.

more thorough.

less difficult.

more manageable.planation:

Explanation: less difficult (C)

8 0
3 years ago
Read 2 more answers
Write a C++ function for the following:
Nana76 [90]

Solution :

class Account

$ \{ $

public:

$\text{Account}()$;

double $\text{getBalance}$();

void $\text{setBalance}$();

$\text{bool withdraw}$(double bal);

$\text{private}:$

double $\text{balance}$;

}:

$\text{Account}()$ {}

double $\text{getBalance}$()

$ \{ $

$\text{return balance}$;

}

void $\text{setBalance}$(double $\text{balance}$)

$ \{ $

this.$\text{balance}$ = $\text{balance}$;

}

$\text{boolean}$ withdraw($\text{double bal}$)

$ \{ $

if($\text{balance}$ >= bal)

$ \{ $

$\text{balance}$ = $\text{balance}$ - bal;

$\text{return}$ true;

}

$\text{return}$ false;

}

}

4 0
2 years ago
Other questions:
  • Plz answer these questions...those who give answer get the brainliest......hurry up..!!!​
    14·1 answer
  • Which registry hive is loaded first during windows startup?
    5·1 answer
  • Which one of the following is not the name of a 20th century school composition
    14·2 answers
  • Which part of a computer takes the text and pictures on your screen and prints them onto paper?
    6·1 answer
  • __________ are the first line of defense against unsafe drivers.
    5·1 answer
  • What is data? why is it important to collect data ? explain the points.​
    10·1 answer
  • Here is a nested loop example that graphically depicts an integer's magnitude by using asterisks, creating what is commonly call
    7·1 answer
  • Where is information stored in the computer?​
    9·1 answer
  • Which part of the computer is responsible for managing memory allocation for all applications
    12·1 answer
  • Briefly describe the working of computer processing system?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!