Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using PyCharm (Python), modify the Gettysburg processing program to generate a text file from an output rather than printing to the screen. Your program should

Using PyCharm (Python), modify the Gettysburg processing program to generate a text file from an output rather than printing to the screen. Your program should have a new function called process_file which prints to the file (this method should almost be the same as the pretty_print function from last week. Keep in mind that we have print statements in main as well. Your program must modify the print statements from main as well. Your program must have a header. Create a new function called process_fie. This function will perform the same operations as pretty_print from week 8 however it will print to a file instead of to the screen. Modify your main method to print the length of the dictionary to the file as opposed to the screen. This will require that you open the file twice. Once in main and once in process_file. Prompt the user for the filename they wish to use to generate the report. Use the filename specified by the user to write the file. This will require you to pass the file as an additional parameter to your new process_file function. The output should be from high to low frequency (text) and use string formatting for this.

File link address: https://content.bellevue.edu/cst/dsc/510/resources/new/gettysburg.txt

Below is the previous code from the previous week, but I'm having trouble figuring out how to write into the file and have it work still!

import string def process_line(line, word_count_dict): """Process the line to get lowercase words to add to the dictionary. """ line = line.strip() word_list = line.split() for word in word_list: # ignore the '' that is in the file if word != '--': word = word.lower() word = word.strip() #get commas, periods, and other punctuation out as well word = word.strip(string.punctuation) add_word(word, word_count_dict) def add_word(word, word_count_dict): """Update the word frequency: word is the key, frequency is the value """ if word in word_count_dict: word_count_dict[word] += 1 else: word_count_dict[word] = 1 def pretty_print(word_count_dict): """Print nicely from highest to lowest frequency """ value_key_list=[] for key,val in word_count_dict.items(): value_key_list.append((val,key)) # sort method sorts on list's first element, the frequency. # Reverse to get biggest first value_key_list.sort(reverse=True) print(value_key_list) # value_key_list = sorted([v,k) for k, v in value_key_list.items()] print('{:11s}{:11s}'.format('Word', 'Count')) print(' '*21) for val,key in value_key_list: print('{:12s} {:<3d}'.format(key,val)) def main(): word_count_dict={} gba_file = open('gettysburg.txt','r') for line in gba_file: process_line(line, word_count_dict) print('Length of the dictionary:',len(word_count_dict)) pretty_print(word_count_dict) if __name__ == "__main__": # execute only if run as a script main()

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

Step: 3

blur-text-image

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

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions

Question

Understand how customers respond to effective service recovery.

Answered: 1 week ago