Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

def read _ file ( file ) : d 1 = { } with open ( file , ' r ' , encoding = 'utf

def read_file(file):
d1={}
with open(file,'r',encoding='utf-8') as infile:
lines = infile.readlines()
for index in range(0, len(lines)-1,2):
if lines[index].strip()=='':
continue
cnt = int(lines[index].strip())
shw = lines[index +1].strip()
if cnt in d1.keys():
sh_list = d1.get(cnt)
sh_list.append(shw)
else:
d1[cnt]=[shw]
return d1
def out_key(d1, filename):
with open(filename,'w+') as q:
for key in sorted(d1.keys()):
q.write('{}: {}
'.format(key,'; '.join(d1.get(key))))
print('{}: {}'.format(key,'; '.join(d1.get(key))))
def out_title(d1, filename):
ttl =[]
for title in d1.values():
ttl.extend(title)
with open(filename,'w+') as outfile:
for title in sorted(ttl):
outfile.write('{}
'.format(title))
print(title)
def main(x):
f_name = x
d1= read_file(f_name)
if d1 is None:
print('Error: Entered file does not exist!: {}'.format(f_name))
return
out_file1= 'output_keys.txt'
out_file2= 'output_titles.txt'
out_key(d1, out_file1)
out_title(d1, out_file2)
print('Entere input file name: ')
user_input = input()
main(user_input)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+ (c) Show that the space is complete.

Answered: 1 week ago