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
Write a program in c++ to read two integers, calculate and print the smallest
Shalnov [3]

Answer:

#include<iostream>

using namespace std;

int main (){

int n1, n2;

cout<<"Enter 1st number";

cin>>n1;

cout<<"Enter 2nd number";

cin>>n2;

if(n1<n2){

cout<<"The 1st number is the smallest"<<endl<<" is= "<<n1;

}

else{

cout<<"The 2nd number is the smallest"<<endl<<" is= "<<n2;

}

}

return 0;

3 0
3 years ago
An image is 160x 42 what is this a measure of?
mixas84 [53]
This would be a measure of Resolution. Image size is the general version of Resolution as Resolution is the measurement use FOR image size.
8 0
3 years ago
Write a BASIC program to find the product of any two numbers​
GuDViN [60]

Answer:

Print 4 + 4

Explanation:

If you're writing this in the programming language called BASIC, this is how you would add any two numbers.

6 0
3 years ago
Sarah is working on a project in which she needs to record all the extracurricular activities in her college. Her college teache
EastWind [94]
It sounds like the answer is A because there are 5 types of sports and the question only asks for the sports folders
8 0
4 years ago
Read 2 more answers
What is a graph,a chart then give similarities between graph and chart.​
Alik [6]

Answer:

A diagram showing the relation between variable quantities, typically of two variables, each measured along one of a pair of axes at right angles. A collection of points whose coordinates satisfy a given relation. A sheet of information in the form of a table, graph, or diagram. Normally graphs and charts in excel are very much similar to each other, but they are different, Graphs are mostly a numerical representation of data as it shows the relation of change in numbers that how one number is affecting or changing another, however, charts are the visual representation where categories may or may not be related to each other also how the information is displayed is different in both graphs and charts.

6 0
3 years ago
Read 2 more answers
Other questions:
  • Pointsfor a failover cluster, what type of network would you use to communicate with an iscsi device?
    15·1 answer
  • If a user has just added a simple triangle shape into a diagram, how can the triangle be adjusted? Check all that apply. by maki
    6·2 answers
  • Why might a peer-to-peer network not be the best choice for a large corporate office setting?
    14·1 answer
  • If Number = 7, what will be displayed after code corresponding to the following pseudocode is run? (In the answer options, new l
    5·1 answer
  • PLS HELP!! 50 Points! Will mark correct answer brainliest!!
    11·2 answers
  • Stella is a bank executive. She is preparing a spreadsheet on the loan repayment schedules of customers. Which function can she
    12·1 answer
  • Information flows:
    11·1 answer
  • Find two consecutive even integers such that twice the smaller is 16 more than the larger
    10·1 answer
  • Why can't I register for Brainly?? I've been trying for months, too! I've noticed this question has been asked so many times, bu
    7·1 answer
  • What is the purpose of extent in lines in engineering drawing
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!