Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This almost worked, but I got a few errors. Code below. Question and errors in photos. def main ( ) : # Step 1 :

This almost worked, but I got a few errors. Code below. Question and errors in photos.
def main():
# Step 1: Read input file name from user
input_file = input("Enter the input file name: ")
# Step 2: Read input file and process data
with open(input_file, 'r') as file:
lines = file.readlines()
# Step 3: Parse input data into dictionary
tv_shows ={}
for i in range(0, len(lines),2):
seasons = int(lines[i].strip())
show_title = lines[i +1].strip()
if seasons in tv_shows:
tv_shows[seasons].append(show_title)
else:
tv_shows[seasons]=[show_title]
# Step 4: Sort dictionary by keys (descending order)
sorted_by_keys = dict(sorted(tv_shows.items(), reverse=True))
# Step 5: Output sorted by keys to output_keys.txt
with open('output_keys.txt','w') as keys_file:
for key, shows in sorted_by_keys.items():
shows_list ='; '.join(shows)
keys_file.write(f"{key}: {shows_list}
")
# Step 6: Sort dictionary by values (reverse alphabetical order)
sorted_by_values = dict(sorted(tv_shows.items(), key=lambda item: item[1], reverse=True))
# Step 7: Output sorted by values to output_titles.txt
with open('output_titles.txt','w') as titles_file:
for key, shows in sorted_by_values.items():
for show in sorted(shows, reverse=True):
titles_file.write(f"{show}
")
if __name__=="__main__":
main() Write a program that first reads in the name of an input file and then reads the input file using the file readlines 0 method. The input file
contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input
file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since multiple shows could have the
same number of seasons).
Sort the dictionary by key (greatest to least) and output the results to a file named output_keys.txt. Separate multiple TV shows associated
with the same key with a semicolon (, ordering by appearance in the input file. Next, sort the dictionary by values (in reverse alphabetical
order), and output the results to a file named output_titles.txt.
Ex: If the input is:
file1.txt
and the contents of file1.txt are:
the file output_keys.txt should contain:
and the file output_titles.txt should contain:
Will & Grace
The Simpsons
Murder, She Wrote
Law & Order
Gunsmoke
Dallas
Note: End each output file with a newline, and file1.txt is available to download. Output differs. See highlights below. Special character legend
Input
Your file content Enter the input file name:
Output differs. See highlights below.
Special character legend
Input file3.txt
image text in transcribed

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

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

8. The number of cases is the number of data points.

Answered: 1 week ago

Question

5.3 Explain internal recruitment methods.

Answered: 1 week ago