Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

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) ) 

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:

print( itemfmt.format(whiteSpace[c], ccount) ) 

Use the string named totalfmt to properly print the total character count. If this count is named total, for example:

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. Here is a picture of what the solution should look like (the left side is the txt file and the right side should be the output we get):

image text in transcribed

This is an intro class so please only use basic coding techniques where possible. An outline of the code is given below, please dont change the outline, but write the code inside of it, thank you:

# CountChars.py # YOUR NAME(S), DATE import 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() 
Enter filename: short.txt char "Being aware is more important than being smart" count Phil Jackson. Sacred Hoops: spiritual lessons of a hardwood warrior. Hyperion Books, 1995. tab nline space 4 18 1 1 13 10 total 143

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions