Answered step by step
Verified Expert Solution
Question
1 Approved Answer
THE CODE THAT YOU GUYS GIVE ME IS NOT WORKING ON MY PROJECT. PLEASE HELP Write a program that first reads in the name of
THE CODE THAT YOU GUYS GIVE ME IS NOT WORKING ON MY PROJECT. PLEASE HELP
Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() 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: 20 Gunsmoke 30 The Simpsons 10 Will \& Grace 14 Dallas 20 Law \& Order 12 Murder, She Wrote the file output_keys.txt should contain: 10: Will \& Grace 12: Murder, She Wrote 14: Dallas 20: Gunsmoke; Law \& Order 30: The Simpsons and the file output_titles.txt should contain: Dallas Gunsmoke Law \& Order Murder, She Wrote The Simpsons \# read the input file name file_name = input("Enter the input file name: ") \# create a dictionary to store the values d={} \# read the contents of the file with open(file_name, "r") as file: lines =file readlines() for line in lines: \# split the line by : and remove the newline character key, value = line.strip().split(': ') key =int(key) if key in d : d[key]. append (value) else: d[key]=[value] \# sort the dictionary by key and write to output_keys.txt with open ("output_keys.txt", "w") as file: for key in sorted(d, reverse=True): values = '; ' join (d[key]) file.write(str(key) +":"+ values + " ") \# sort the dictionary by value and write to output_titles.txt with open("output_titles.txt", "w") as file: for value in sorted (d.values(), key=lambda x:x[0], reverse=True): file.write (value [0]+ " ") 1:Compare output 0/1 Enter the input file name: Could not find file: output keys.txt Traceback (most recent call last): File "main.py", line 12 , in key, value = line.strip().split(': ') ValueError: not enough values to unpack (expected 2, got 1) Input Your file content Your program produced no output 30: The Simpsons 20: Gunsmoke; Law \& Order 14: Dallas 12: Murder, She Wrote 10: Will \& Grace 2:Compare output 0/1 Enter the input file name: Could not find file: output_titles.txt Traceback (most recent call last) : File "main.py", line 12 , in key, value = line.strip().split(': ') ValueError: not enough values to unpack (expected 2, got 1) Input Your file content Your program produced no output Will \& Grace The Simpsons Murder, She Wrote Law \& Order Gunsmoke Dallas 3:Compare output 0/2 Enter the input file name: Could not find file: output_keys.txt Traceback (most recent call last): File "main.py", line 12, in
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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