Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!