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
SOMEONE PLEASE HELP ME I REPOSTED THIS 3 time and no ONE HAD HELPED ME
RoseWind [281]

Answer:

The answer is B

7 0
3 years ago
Once you fix the bug in a code, what is the last thing you should do?
aleksklad [387]

Answer:

Know what the code should do

4 0
2 years ago
What is an example of a reputable website
Keith_Richards [23]
A reputable website would be one that you could research online and read about it's history , reviews etc .
4 0
3 years ago
Read 2 more answers
true of false one reason to move to a paperless society is that printers are becoming prohibitively expensive
Yakvenalex [24]
True because printers, which require paper, are becoming more expensive. A paperless society has the advantage of being cheaper in this aspect.
5 0
3 years ago
How do you draw first angle of orthographic projection ​
attashe74 [19]
Https://technologystudent.com/designpro/ortho1.htm
5 0
2 years ago
Other questions:
  • What is the recommended size for bulleted text? 12–22 24–40 44–66 54–80
    10·1 answer
  • Universal Containers has implemented a strict software architecture for their custom Apex code. One of the requirements is that
    14·1 answer
  • . the web is based on the ________ protocol
    6·1 answer
  • When inserting a fly in animation what is the first step in the process?
    9·1 answer
  • If you have a windows 8, but want the features of windows 8 pro, purchase and install _______.
    7·1 answer
  • A web designer is creating a web site and wants each browser to resize the text size to look the same in each device. what measu
    6·1 answer
  • Create a function names minElement() that takes an array of numbers as a paaramter and returns the min value of the array. The a
    9·1 answer
  • 6. According to camp policy, staff members need to be at least 23 years old to transport students. Dean now wants to determine h
    9·1 answer
  • Changing how information is represented so that it can be read by a computer is called
    7·1 answer
  • The process of arranging the item of a column in some sequence or order is known as?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!