6.10 LAB: Sorting TV Shows (dictionaries and lists)   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 (least to greatest) 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 (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 Will & Grace Note: There is a newline at the end of each output file, and file1.txt is available to download. MY CODE: filename=input('') dict_info={} with open(filename, 'r') as infile:     lines=infile.readlines()     for i in range(0, len(lines) -1, 2):         if lines [i].strip()==' ':continue         count=int(lines[i].strip())         name=lines[i+1].strip()         if count in dict_info.keys():                 names=dict_info.get(count)                 names.append(name)                 names.sort()         else:             dict_info[count]=[name]             print(count, name)                  if dict_info is None:         print('{}'.format(file))              outFile1 = 'output_keys.txt'     outFile2 = 'output_titles.txt'          with open(outFile1, 'w+') as output:         for i in sorted(dict_info.keys()):             output.write('{}: {}\n'.format(i,'; '.join(dict_info.get(i))))             print('{}: {}\n'.format(i,'; '.join(dict_info.get(i))))     titleNames=[]     for i in dict_info.values():         titleNames.extend(i)     with open(outFile2, 'w+') as output:                  for i in sorted(titleNames):             output.write('{}\n'.format(i))             print(i)              Output is not correct.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter14: Introduction To Swing Components
Section: Chapter Questions
Problem 1GZ
icon
Related questions
Question

6.10 LAB: Sorting TV Shows (dictionaries and lists)

 

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 (least to greatest) 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 (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 Will & Grace

Note: There is a newline at the end of each output file, and file1.txt is available to download.

MY CODE:

filename=input('')
dict_info={}
with open(filename, 'r') as infile:
    lines=infile.readlines()
    for i in range(0, len(lines) -1, 2):
        if lines [i].strip()==' ':continue
        count=int(lines[i].strip())
        name=lines[i+1].strip()
        if count in dict_info.keys():
                names=dict_info.get(count)
                names.append(name)
                names.sort()
        else:
            dict_info[count]=[name]
            print(count, name)
            
    if dict_info is None:
        print('{}'.format(file))
        
    outFile1 = 'output_keys.txt'
    outFile2 = 'output_titles.txt'
    
    with open(outFile1, 'w+') as output:
        for i in sorted(dict_info.keys()):
            output.write('{}: {}\n'.format(i,'; '.join(dict_info.get(i))))
            print('{}: {}\n'.format(i,'; '.join(dict_info.get(i))))
    titleNames=[]
    for i in dict_info.values():
        titleNames.extend(i)
    with open(outFile2, 'w+') as output:
        
        for i in sorted(titleNames):
            output.write('{}\n'.format(i))
            print(i)
            

Output is not correct. 

Output differs. See highlights below. Special character legend
Input
file2.txt
7: Lux Video Theatre; Medium; Rules of Engagement
8: Barney Miller; Castle; Mama
10: Friends; Modern Family; Smallville; Will & Grace
11: Cheers; The Jeffersons
Your file content
12: Murder, She Wrote; NYPD Blue
14: Bonanza; Dallas
15: ER
20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit
30: The Simpsons
7: Rules of Engagement; Medium; Lux Video Theatre
8: Mama; Barney Miller; Castle
10: Will & Grace; Smallville; Modern Family; Friends
11: Cheers; The Jeffersons
Expected file content
12: Murder, She Wrote; NYPD Blue
14: Dallas; Bonanza
15: ER
20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit
30: The Simpsons
Transcribed Image Text:Output differs. See highlights below. Special character legend Input file2.txt 7: Lux Video Theatre; Medium; Rules of Engagement 8: Barney Miller; Castle; Mama 10: Friends; Modern Family; Smallville; Will & Grace 11: Cheers; The Jeffersons Your file content 12: Murder, She Wrote; NYPD Blue 14: Bonanza; Dallas 15: ER 20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit 30: The Simpsons 7: Rules of Engagement; Medium; Lux Video Theatre 8: Mama; Barney Miller; Castle 10: Will & Grace; Smallville; Modern Family; Friends 11: Cheers; The Jeffersons Expected file content 12: Murder, She Wrote; NYPD Blue 14: Dallas; Bonanza 15: ER 20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit 30: The Simpsons
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage