1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
zvonat [6]
3 years ago
14

Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. T

he 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).
Computers and Technology
1 answer:
irinina [24]3 years ago
3 0

Answer:

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)

main()

Explanation:

You might be interested in
Discuss briefly general-purpose graphicsprimitives that contain 2D graphics library.
Zina [86]

Answer:

  General purpose graphics primitives that contain 2D graphic library as, it is used in graphics processing unit which typically manage computation for computer graphics. Graphic primitives contain basic element such as lines and curves. Modern 2D computer graphic system that operate with primitives which has lines and shapes. All the elements of graphic are formed from primitives.

8 0
3 years ago
What do rocket scientists mean when they say, "forces come in pairs?"
zhenek [66]
According to Newton's third law, for every action force there is an equal (in size) and opposite (in direction) reaction force.
6 0
3 years ago
Websites using www showed up on the internet for the first time in -992
pogonyaev
1991 was the year websites first started to use www. (world wide web)
8 0
3 years ago
Which computer program can you use to write, edit, print, and perform any modifications to a document?
faltersainse [42]
D. word-processing software
4 0
4 years ago
Read 2 more answers
Please help .........​
Verdich [7]

Answer:

I don't think brainly can help on this one chief I think you will have to research the topic and write about it yourself. or just write 3 facts and put spaces until you reach the limit.

3 0
3 years ago
Other questions:
  • Task:create a struct that looks like:typedef struct person{ int age; double height;} Person;Create an array of Persons. Read dat
    13·1 answer
  • Describe the functions of ALU, resisters, CU, BIU, Cache and FPU.
    7·1 answer
  • On net, is technology good or bad for society? Please explain, support your argument, I’ll mark brainliest
    9·2 answers
  • a sign is to be hung from the end of a thin pole, and the pole supported by a single cable. your design firm brainstorms the six
    13·1 answer
  • Which steps do you follow to create a graph?
    5·2 answers
  • Cual es la constante de proporcionalidad de la función f(x)= 4x-6 ​
    7·1 answer
  • To cope with the uncertainty about how their pages will be viewed, many web page designers opt to use _________ units, which are
    13·1 answer
  • What happens after the initial rollout of a new network generation?
    10·1 answer
  • Becca is working on a program that will store data. The program will need quick access to data and data persistence is not impor
    15·1 answer
  • 20 ponts!!!dorothy has made a website for her hair salon on a wysiwyg editor. she wants her website to appear on the first page
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!