Question

Character Count Write a program that reads a file and counts the number of occurrences of...

Character Count

Write a program that reads a file and counts the number of occurrences of each character in the file (case sensitive). The file name is "char.txt". You should use a dictionary to hold the number of occurrences of each character, and print out that dictionary.

For example, if the file contains only six characters, "tactic", your dictionary should contain the following key-value pairs (order can be different):

{'a': 1, 'c': 2, 'i': 1, 't': 2}

Sample Input:

Follow the instructions:

1. In your Jupyter Notebook, create a new empty text file (this will be the file you will read from);

2. Download the following text file:

char.txt

3. Use your default text editor to open char.txt;

4. Copy all the data in char.txt to the new text file you created in Jupyter Notebook;

5. Rename the new text file opened in Jupyter Notebook as char.txt;

6. Save the new text file in Jupyter Notebook.

Sample Output:

{'L': 1, 'O': 2, 'N': 2, 'D': 1, '—': 1, 'B': 3, 'r': 22, 'i': 16, 't': 22, 's': 11, 'h': 9, ' ': 45, 'l': 6, 'a': 16, 'w': 1, 'm': 5, 'k': 2, 'e': 35, 'v': 5, 'o': 20, 'd': 9, 'S': 1, 'u': 7, 'y': 5, 'p': 4, 'n': 16, 'c': 5, 'x': 2, ',': 1, 'f': 5, 'g': 3, 'P': 1, 'M': 1, 'J': 1, 'q': 1, 'U': 2, '.': 3, 'K': 1, '’': 1, 'E': 1, 'b': 1}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Char.txt:

Data Explorer is a dashboard application that can automate basic machine learning tasks. The aim is to build a unique tool for the users where Data Input, Preprocessing, Cleaning, Exploring and application of machine learning algorithms take place in a single_tool. With the help of our tool, the user will get a clear glimpse of what machine learning is, and also to get a clear view of how the data is being explored, cleaned, viewed and stored according to the user’s choice without the use of computer programming.

Char_count_file.py:

   #python code to demonstrate each character occurance frequency in a given file.
   def main():
       #opens the file back
       f = open("char.txt","r")
       #reads the contents of the file
       f1 = f.read()
       #creating a diictionary with name 'res'to hold the occurances of the each character
       res = {}
       #loops through the file
       for keys in f1:
           #using dict.get() to get count of each element in a string.
           res[keys] = res.get(keys, 0)+1
       #printing result as dictionary   
       print(str(res))
   if __name__=="__main__":
       main()

  

Output:

C Character Count Write A Program * Echar_count_ file x chartxt x + - 0 x f = c localhost:8888/notebooks/char_count_file.ipyn

Add a comment
Know the answer?
Add Answer to:
Character Count Write a program that reads a file and counts the number of occurrences of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT