Question: can someone help me finish this using for, while, dictionary, or things like that, do not use enumerate ,strip, break CountChars.py (40 points: reading from
can someone help me finish this using for, while, dictionary, or things like that, do not use enumerate ,strip, break
CountChars.py
(40 points: reading from file or URL, formatting strings, data structures)
Begin a new file named CountChars.py by typing a comment at the top with your name (and the name of your lab partner if you work together), and the current date.
You will notice the instructions are a bit shorter this time. You are given a problem to solve, but you must figure out how to solve it yourself. Keep in mind what you have learned about top-down programming by stepwise refinement: break the problem into meaningful parts; then solve each part separately. And have fun!
The problem is to write a Python3 program that reads a text file or web page named by the user, and prints a neat table showing the counts of all of the different characters it reads. You must also meet the following explicit specifications:
Since we insist that your output will exactly match the results of our solution (see the 3 examples below), copy the following string and dictionary constants and paste them to the beginning of countchars.py (after your header comment of course). These are already entered for you in the start-up file called CountChars.py.
# strings that must be used for output: prompt = "Enter filename: " titles = "char count ---- -----" itemfmt = "{0:5s}{1:10d}" totalfmt = "total{0:10d}" whiteSpace = {' ':'space', '\t':'tab', ' ':'nline', ' ':'crtn'} Make it so that when you run the module in IDLE, it should immediately instruct the user to enter the filename, and then it should print the table of counts in the Python shell window. Alternatively, a user can run the program directly from the command prompt without ever starting Python or IDLE, by typing the following:
-bash-4.2$ python3 CountChars.py
Execution must proceed as follows:
print( itemfmt.format(c, ccount) )
print( itemfmt.format(whiteSpace[c], ccount) )
print( totalfmt.format(total) )
Use the built-in function input to get the filename from the user. Pass the string named prompt to this function.
If the filename does not begin with "http" then assume it is a local file, and open it for reading with the built-in open function. Otherwise import and use the urllib.request.urlopen function to open the web page for reading. The program has to be able to do this automatically. Go back to the slides from class for when we talked about this topic.
Read all of the text in the file or web page, and count each different character in it. Ignore the case of characters (e.g., count both 'A' and 'a' as 'a').
Print the string named titles. Then print the table of characters and their counts in ASCII order, and print the total character count at the bottom. Hint: There are 128 characters in the ASCII code, starting from 0 and ending at 127.
Use the string named itemfmt to print each interior row of the table in the proper format. For example, if c is a character, and ccount is its count (which should be recorded in the dictionary you created), then print that row of the table as follows:
If the character being printed is one of the white space characters in the dictionary named whitespace, then print its description instead of the character itself. For example:
Use the string named totalfmt to properly print the total character count. If this count is named total, for example:
Fully test your program. Here are sample input files and web pages, and the associated results from our solution. Make sure that your results exactly match these results. Also realize that the graders will probably test other inputs too.
mport urllib.request
# This function reads all the lines, reads all the characters, makes a dictionary def MakeD(xfile): # STUDENTS: FINISH THIS PART
def main(): # strings that must be used for output: prompt = "Enter filename: " titles = "char count ---- -----" itemfmt = "{0:5s}{1:10d}" totalfmt = "total{0:10d}" whiteSpace = {' ':'space', '\t':'tab', ' ':'nline', ' ':'crtn'}
# Get file name and see if you need to use open or urlopen # Call the function MakeD as appropriate # STUDENTS: FINISH THIS PART
# Print out contents of dictionary print(titles) # STUDENTS: FINISH THIS PART # Hint: go thru all 128 ASCII characters and see if they're in the dictionary # Follow instructions closely
if __name__ == "__main__": main()
codes abvoe are what have been provided
thanks so much!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
