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. **Code** 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 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('{}: {}\n'.format(key,';'.join(dictn.get(key)))) 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) def main(): file_Name = input() dictn = readFile(file_Name) if dictn is None: print('Error: Invalid file name provided: {}'.format(file_Name)) return print(dictn) output_file_Name_1 ='output_keys.txt' output_file_Name_2 ='output_titles.txt' output_keys(dictn,output_file_Name_1) output_titles(dictn,output_file_Name_2) main() **The output for this code has an extra line and some extra spacing that I dont need. Output is below. 20 Gunsmoke 30 The Simpsons 10 Will & Grace 14 Dallas 20 Law & Order 12 Murder, She Wrote {20: ['Gunsmoke', 'Law & Order'], 30: ['The Simpsons'], 10: ['Will & Grace'], 14: ['Dallas'], 12: ['Murder, She Wrote']} **This is not needed** 10: Will & Grace 12: Murder, She Wrote 14: Dallas 20: Gunsmoke;Law & Order 30: The Simpsons *** this section and up to the last section "not needed" has to many spaces** Dallas Gunsmoke Law & Order Murder, She Wrote The Simpsons Will & Grace
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.
**Code**
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
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('{}: {}\n'.format(key,';'.join(dictn.get(key))))
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)
def main():
file_Name = input()
dictn = readFile(file_Name)
if dictn is None:
print('Error: Invalid file name provided: {}'.format(file_Name))
return
print(dictn)
output_file_Name_1 ='output_keys.txt'
output_file_Name_2 ='output_titles.txt'
output_keys(dictn,output_file_Name_1)
output_titles(dictn,output_file_Name_2)
main()
**The output for this code has an extra line and some extra spacing that I dont need. Output is below.
20 Gunsmoke
30 The Simpsons
10 Will & Grace
14 Dallas
20 Law & Order
12 Murder, She Wrote
{20: ['Gunsmoke', 'Law & Order'], 30: ['The Simpsons'], 10: ['Will & Grace'], 14: ['Dallas'], 12: ['Murder, She Wrote']} **This is not needed**
10: Will & Grace
12: Murder, She Wrote
14: Dallas
20: Gunsmoke;Law & Order
30: The Simpsons *** this section and up to the last section "not needed" has to many spaces**
Dallas
Gunsmoke
Law & Order
Murder, She Wrote
The Simpsons
Will & Grace
Trending now
This is a popular solution!
Step by step
Solved in 6 steps with 5 images