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, separating multiple TV shows associated with the same key with a semicolon (;). Next, sort the dictionary by values (alphabetical order), and output the results to a file named output_titles.txt. Ex: If the input is:

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

In Python:

 

I've received a few errors, what could I be doing wrong? 

 

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, separating multiple TV shows associated with the same key with a semicolon (;). 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

 

 

My Code:

#Define readFile() function

def readFile(file_Name):

dictn = {}

with open(file_Name, 'r') as inp_File:

lines = inp_File.readlines()

for index in range(0, len(lines) - 1, 2):

if lines[index].strip()=='':continue

cnt = int(lines[index].strip())

name = lines[index + 1].strip()

if cnt in dictn.keys():

name_list = dictn.get(cnt)

name_list.append(name)

name_list.sort()

else:

dictn[cnt] = [name]

print(cnt,name)

return dictn

 

#Define output_keys() function

def output_keys(dictn, file_Name):

with open(file_Name,'w+') as out_File:

for key in sorted(dictn.keys()):

out_File.write('{}: {}\n'.format(key,';'.join(dictn.get(key))))

print('{}: {}'.format(key,';'.join(dictn.get(key))))

 

#Define output_titles() function

def output_titles(dictn, file_Name):

titles = []

for title in dictn.values():

titles.extend(title)

with open(file_Name,'w+') as out_File:

for title in sorted(titles):

out_File.write('{}\n'.format(title))

print(title)

 

#Define main() function

def main():

file_Name = input('Enter the file name:')

dictn = readFile(file_Name)

if dictn is None:

print('Error: Invalid file name provided: {}'.format(file_Name))

return

output_file_Name_1 ='output_keys.txt'

output_file_Name_2 ='output_titles.txt'

print("\nKeys:")

output_keys(dictn,output_file_Name_1)

print('\nTitles:')

output_titles(dictn,output_file_Name_2)

main()

zy IT 140: Introduction to Scripting
zy Section 7.9 - IT 140: Introduction x
+
A learn.zybooks.com/zybook/SNHUIT140V3/chapter/7/section/9
My library > IT 140: Introduction to Scripting v3 home >
= zyBooks
8 zyBooks catalog
? Help/FAQ
8 Jillian Alexander
7.9: LAB: Sorting TV Shows (dictionaries and lists)
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
4: Compare output a
2/2
• O 7:52
Transcribed Image Text:zy IT 140: Introduction to Scripting zy Section 7.9 - IT 140: Introduction x + A learn.zybooks.com/zybook/SNHUIT140V3/chapter/7/section/9 My library > IT 140: Introduction to Scripting v3 home > = zyBooks 8 zyBooks catalog ? Help/FAQ 8 Jillian Alexander 7.9: LAB: Sorting TV Shows (dictionaries and lists) 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 4: Compare output a 2/2 • O 7:52
zy IT 140: Introduction to Scripting
zy Section 7.9 - IT 140: Introduction x
+
A learn.zybooks.com/zybook/SNHUIT140V3/chapter/7/section/9
My library > IT 140: Introduction to Scripting v3 home >
= zyBooks
E zyBooks catalog
e Help/FAQ
8 Jillian Alexander
7.9: LAB: Sorting TV Shows (dictionaries and lists)
Titles:
Dallas
Gunsmoke
Law & Order
Murder, She Wrote
The Simpsons
Will & Grace
Output is nearly correct; but whitespace differs. See highlights below.
Special character legend
Input
file1.txt
10: Will & Grace
12: Murder, She Wrote
Your file content
14: Dallas
20: Gunsmoke;Law & Order
30: The Simpsons
10: Will & Grace
12: Murder, She Wrote
Expected file content
14: Dallas
20: Gunsmoke; Law & Order
30: The Simpsons
2: Compare output a
1/1
• 1 7:52
Transcribed Image Text:zy IT 140: Introduction to Scripting zy Section 7.9 - IT 140: Introduction x + A learn.zybooks.com/zybook/SNHUIT140V3/chapter/7/section/9 My library > IT 140: Introduction to Scripting v3 home > = zyBooks E zyBooks catalog e Help/FAQ 8 Jillian Alexander 7.9: LAB: Sorting TV Shows (dictionaries and lists) Titles: Dallas Gunsmoke Law & Order Murder, She Wrote The Simpsons Will & Grace Output is nearly correct; but whitespace differs. See highlights below. Special character legend Input file1.txt 10: Will & Grace 12: Murder, She Wrote Your file content 14: Dallas 20: Gunsmoke;Law & Order 30: The Simpsons 10: Will & Grace 12: Murder, She Wrote Expected file content 14: Dallas 20: Gunsmoke; Law & Order 30: The Simpsons 2: Compare output a 1/1 • 1 7:52
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY