question archive Write a program that first reads in the name of an input file and then reads the input file using the file

Write a program that first reads in the name of an input file and then reads the input file using the file

Subject:Computer SciencePrice:5.87 Bought8

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

 

THIS IS WHAT I HAVE

file = input()

with open(file) as f:

data = f.readlines()

dict_info = {}

for i in range(0,len(data)-1, 2):

season = data[i].strip()

name = data[i+1].strip()

if(season in dict_info):

dict_info[season].append(name)

else:

dict_info[season] = [name]

keys = list(dict_info.keys())

keys.sort()

with open('output_keys.txt', 'w') as f:

for key in keys:

names = '; '.join(name for name in dict_info[key])

f.write(str(key)+': '+names+"n")

names = []

for item in dict_info:

for name in dict_info[item]:

names.append(name)

names.sort()

with open('output_titles.txt', 'w') as f:

for name in names:

f.write(name+'n')

 

 

Input

file2.txt

Your output

07: Rules of Engagement; Medium; Lux Video Theatre

08: Mama; Barney Miller; Castle

10: Will & Grace; Smallville; Modern Family; Friends

11: Cheers; The Jeffersons

12: Murder, She Wrote; NYPD Blue

14: Dallas; Bonanza

15: ER

20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit

30: The Simpsons

 

Expected output

7: Rules of Engagement; Medium; Lux Video Theatre

8: Mama; Barney Miller; Castle

10: Will & Grace; Smallville; Modern Family; Friends

11: Cheers; The Jeffersons

12: Murder, She Wrote; NYPD Blue

14: Dallas; Bonanza

15: ER

20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit

30: The Simpsons

Option 1

Low Cost Option
Download this past answer in few clicks

5.87 USD

PURCHASE SOLUTION

Option 2

Custom new solution created by our subject matter experts

GET A QUOTE

rated 5 stars

Purchased 8 times

Completion Status 100%