Question
Write a PYTHON program that reads a text file and counts how many occurrences of each character are there in the text. The user may
Write a PYTHON program that reads a text file and counts how many occurrences of each character are there in the text. The user may ask the program to ignore some characters and those to be ignored will also be specified in a file.
The program does the following:
1. Prompt the user for the name of the file that contains the text to be examined.
2. Ask the user if they want to ignore some characters. If the user answers yes, the program should ask for the name of the file containing the set of characters to be ignored from the frequency count.
3. Read all the characters from the file and build a Python dictionary that contains each character found in the file and relate it to an integer that tells how many times that character appeared in the file.
4. After processing the text file and building the dictionary, the program will print the most frequent characters with a count of how many times they appear in the text le. 5. Will control the exceptions that may happen in the program, by using the constructs
try and except.
Example output transcript of program (User input
is in italics.):
1. User provides the name of the file on which to count the character occur- rences:
What is the name of the file to be examined?
textfile.txt
Do you want to ignore some characters?
No
The most frequent character was the space with 30 occurrences.
2. Users provides name of the input file and name of the file with to be ignored characters:
What is the name of the file to be examined?
textfile.txt
Do you want to ignore some characters?
Yes
What is the name of the file with the characters to be ignored?
ignoreChars.txt
The most frequent character was the letter a with 13 occurrences.
Requirements:
- The frequency count for the characters must be stored in a Python dictionary.
- The text to be analyzed and the characters to be ignored from the text must be read from two separate files.
Step by Step Solution
3.45 Rating (155 Votes )
There are 3 Steps involved in it
Step: 1
from os import sys fileinputenter the name of the file if lenfile 1 printc...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started