question archive Write a program that first reads in the name of an input file and then reads the input file using the file

Write a program that first reads in the name of an input file and then reads the input file using the file

Subject:Computer SciencePrice:5.87 Bought8

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

 

THIS IS WHAT I HAVE

file = input()

with open(file) as f:

data = f.readlines()

dict_info = {}

for i in range(0,len(data)-1, 2):

season = data[i].strip()

name = data[i+1].strip()

if(season in dict_info):

dict_info[season].append(name)

else:

dict_info[season] = [name]

keys = list(dict_info.keys())

keys.sort()

with open('output_keys.txt', 'w') as f:

for key in keys:

names = '; '.join(name for name in dict_info[key])

f.write(str(key)+': '+names+"n")

names = []

for item in dict_info:

for name in dict_info[item]:

names.append(name)

names.sort()

with open('output_titles.txt', 'w') as f:

for name in names:

f.write(name+'n')

 

 

Input

file2.txt

Your output

07: Rules of Engagement; Medium; Lux Video Theatre

08: Mama; Barney Miller; Castle

10: Will & Grace; Smallville; Modern Family; Friends

11: Cheers; The Jeffersons

12: Murder, She Wrote; NYPD Blue

14: Dallas; Bonanza

15: ER

20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit

30: The Simpsons

 

Expected output

7: Rules of Engagement; Medium; Lux Video Theatre

8: Mama; Barney Miller; Castle

10: Will & Grace; Smallville; Modern Family; Friends

11: Cheers; The Jeffersons

12: Murder, She Wrote; NYPD Blue

14: Dallas; Bonanza

15: ER

20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit

30: The Simpsons

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:
def main():
    file = input('Enter input file name: ') #get file nae 
    dict_info = {} #data dictionary
    with open(file, 'r') as infile:
        lines = infile.readlines() #get file data 
        for i in range(0, len(lines) - 1, 2):
            if lines[i].strip()=='':continue #strip down the empty space 
            count = int(lines[i].strip())
            name = lines[i + 1].strip()
            if count in dict_info.keys():
                names = dict_info.get(count) #get data 
                names.append(name)
                names.sort() #sort them 
            else:
                dict_info[count] = [name]
            print(count,name) 


    if dict_info is None:
        print('Error: Invalid file name provided: {}'.format(file))
        return
    outFile1 ='output_keys.txt' 
    outFile2 ='output_titles.txt'
    #print data into files 
    with open(outFile1,'w+') as output:
      for i in sorted(dict_info.keys()):
          output.write('{}: {}\n'.format(i,';'.join(dict_info.get(i))))
          print('{}: {}\n'.format(i,';'.join(dict_info.get(i))))
    titleNames = []
    for i in dict_info.values():
        titleNames.extend(i)
    with open(outFile2,'w+') as output:


        for i in sorted(titleNames):
            output.write('{}\n'.format(i))
            print(i)
    



main()

PFA

Related Questions