def main(): file = input('Enter input file name: ') #get file nae dict_info = {} #data dictionary with open(file, 'r') as infile: lines = infile.readlines() #get file data for i in range(0, len(lines) - 1, 2): if lines[i].strip()=='':continue #strip down the empty space count = int(lines[i].strip()) name = lines[i + 1].strip() if count in dict_info.keys(): names = dict_info.get(count) #get data names.append(name) names.sort() #sort them else: dict_info[count] = [name] print(count,name)
if dict_info is None: print('Error: Invalid file name provided: {}'.format(file)) return outFile1 ='output_keys.txt' outFile2 ='output_titles.txt' #print data into files with open(outFile1,'w+') as output: for i in sorted(dict_info.keys()): output.write('{}: {} '.format(i,';'.join(dict_info.get(i)))) print('{}: {} '.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('{} '.format(i)) print(i)
main() # Start the execution
12.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, 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 Note: There is a newline at the end of each output file, and file1.txt is available to download. 2996721716238.qx3zqy7 LAB ACTIVITY 12.10.1: LAB: Sorting TV Shows (dictionaries and lists) 6/10 Downloadable files file1.txt Download main.py Load default template... 1 # Type your code here Submit for grading Signature of your work What is this? 2/18.. R------10--15-------10101116101016101016- 1010-----161 6-101010- 0- M-15 W0101010 ..3/10 Latest submission - 7:24 PM PST on 03/10/21 Total score: 0/10 Download this submission Only show failing tests 1: Compare output 0/1 Could not find file: output_keys.txt File "main.py", line 3 dic = 0) IndentationError: expected an indented block Input filel.txt Your file content your program produced no output 10: Will & Grace 12: Murder, She Wrote Expected file content 14: Dallas 20: Gunsmoke; Law & Order 30: The Simpsons 2: Compare output A 0/1 Could not find file: output_titles.txt File "main.py", line 3 dic = 0) IndentationError: expected an indented block Input file1.txt Your file content your program produced no output Dallas Gunsmoke Law & Order Expected file content Murder, She Wrote The Simpsons Will & Grace 3: Compare output 0/2 Could not find file: output_keys.txt File "main.py", line 3 dic = 0) IndentationError: expected an indented block Input file2.txt Your file content your program produced no 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 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 0/2 Could not find file: output_titles.txt File "main.py", line 3 dic = [] IndentationError: expected an indented block Input file2.txt Your file content your program produced no output Barney Miller Bonanza Castle Cheers Dallas ER Friends Gunsmoke Law & Order Law & Order: Special Victims Unit Expected file content Lux Video Theatre Mama Medium Modern Family Murder, She Wrote NYPD Blue Rules of Engagement Smallville The Jeffersons The Simpsons 5: Compare output 0/2 Could not find file: output_keys.txt File "main.py", line 3 dic = {} IndentationError: expected an indented block Input file3.txt Your file content your program produced no output 7: Rules of Engagement; Medium; Lux Video Theatre; Step by Step; Golden 8: Mama; Barney Miller; Castle; Perfect Strangers 9: Perry Mason; CSI: NY; The Middle 10: Will & Grace; Smallville; Modern Family; Friends 11: Cheers; The Jeffersons 12: Murder, She Wrote; NYPD Blue; Bones; The Big Bang Theory Expected file content 14: Dallas; Bonanza; King of the Hill; Knots Landing; Armstrong Circle 1 15: ER; The Jack Benny Program; American Dad!; Grey's Anatomy 16: NCIS 17: Family Guy 20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit 30: The Simpsons 6: Compare output 0/2 Could not find file: output_titles.txt File "main.py", line 3 dic = ! IndentationError: expected an indented block Input file3.txt Your file content your program produced no output American Dad! Armstrong Circle Theatre Barney Miller Bonanza Bones CSI: NY Castle Cheers Dallas ER Family Guy Friends Golden Girls Grey's Anatomy Gunsmoke King of the Hill Knots Landing Law & Order Law & Order: Special Victims Unit Expected file content Lux Video Theatre Mad About You Mama Medium Modern Family Murder, She Wrote NCIS NYPD Blue Perfect Strangers Perry Mason Rules of Engagement Smallville Step by Step The Big Bang Theory The Jack Benny Program The Jeffersons The Middle The Simpsons Will & Grace