subject

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.
My current code:
def readFile(filename):
dict = {}
with open(filename, 'r') as infile:
lines = infile. readlines()
for index in range(0, len(lines) - 1, 2):
if lines[index].strip()=='':continue count = int(lines[index].strip())
name = lines[index + 1].strip()
if count in dict. keys():
name_list = dict. get(count)
name_list. append(name)
name_list. sort()
else:
dict[count] = [name]
print(count, name)
return dict
def output_keys(dict, filename):
with open(filename,'w+') as outfile:
for key in sorted(dict. keys()):
outfile. write('{}: {}\n'.format(key,'; '.join(dict. get(key
print('{}: {}\n'.format(key,';'.join(dict. get(key
def output_titles(dict, filename):
titles = []
for title in dict. values():
titles. extend(title)
with open(filename,'w+') as outfile:
for title in sorted(titles):
outfile. write('{}\n'.format(title))
print(title)
def main():
filename = input('Enter input file name: ')
dict = readFile(filename)
if dict is None:
print('Error: Invalid file name provided: {}'.format(filename))
return
print(dict)
output_filename_1 ='output_keys. txt'
output_filename_2 ='output_titles. txt'
output_keys(dict, output_filename_1)
output_titles(dict, output_filename_2)

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 09:40
It is vital to research each of the services you plan to disable before implementing any change, especially on critical machines such as the: a. servers in the test environment. b. domain controller and other infrastructure servers. c. desktops that have previously been attacked. d. desktops used by upper-level management.
Answers: 2
question
Computers and Technology, 22.06.2019 11:10
Look at the far left lane in the picture. explain what the red car is doing and what it needs to do to travel safely.
Answers: 2
question
Computers and Technology, 23.06.2019 02:00
Consider the following function main: int main() { int alpha[20]; int beta[20]; int matrix[10][4]; . . } a. write the definition of the function inputarray that prompts the user to input 20 numbers and stores the numbers into alpha. b. write the definition of the function doublearray that initializes the elements of beta to two times the corresponding elements in alpha. make sure that you prevent the function from modifying the elements of alpha. c. write the definition of the function copyalphabeta that stores alpha into the first five rows of matrix and beta into the last five rows of matrix. make sure that you prevent the function from modifying the elements of alpha and beta. d. write the definition of the function printarray that prints any onedimensional array of type int. print 15 elements per line. e. write a c11 program that tests the function main and the functions discussed in parts a through d. (add additional functions, such as printing a two-dimensional array, as needed.)
Answers: 3
question
Computers and Technology, 23.06.2019 02:30
What is the power dissipated by a resistor with a current of 0.02 a and a resistance of 1,000 ? a. 200 w b. 20 w c. 0.4 w d. 4 w
Answers: 1
You know the right answer?
Write a program that first reads in the name of an input file and then reads the input file using th...
Questions
question
Mathematics, 22.07.2019 06:50
Questions on the website: 13722360