Question: Write a program that first reads in the name of an input file and then reads the input file using the file.readlines ( ) method.
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 outputkeys.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 outputtitles.txt
Ex: If the input is:
filetxt
and the contents of filetxt are:
Gunsmoke
The Simpsons
Will & Grace
Dallas
Law & Order
Murder, She Wrote
the file outputkeys.txt should contain:
: Will & Grace
: Murder, She Wrote
: Dallas
: Gunsmoke; Law & Order
: The Simpsons
and the file outputtitles.txt should contain:
Dallas
Gunsmoke
Law & Order
Murder, She Wrote
The Simpsons
Will & Grace
I have this so far and it is so close I just need to get rid of the zeros in front of and filename input
f openfilenamer
data freadlines
fclose
dictionary
listofvalues set
for i in rangelendata:
datai dataistrip
for i in range lendata:
key datai
value datai
if key in dictionary:
if value not in dictionarykey:
dictionarykey; value
listofvalues.addvalue
else:
dictionarykey value
listofvalues.addvalue
with openoutputkeys.txtw as f:
for key, value in sorteddictionaryitems:
fwritefkey: value
with openoutputtitles.txtw as f:
for value in sortedlistofvalues:
fwritefvalue
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
