Advertisement
biswasrohit20

seasons

Apr 17th, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. def read_da_file(something):
  2. dict1 = {}
  3. with open(something, 'r') as infile:
  4. lines = infile.readlines()
  5. for index in range(0, len(lines) - 1, 2):
  6. if lines[index].strip() == '':
  7. continue
  8. count = int(lines[index].strip())
  9. show = lines[index + 1].strip()
  10. if count in dict1.keys():
  11. show_list = dict1.get(count)
  12. show_list.append(show)
  13. else:
  14. dict1[count] = [show]
  15.  
  16. return dict1
  17.  
  18.  
  19. def output_keys(dict1, filename):
  20. with open(filename, 'w+') as q:
  21. for key in sorted(dict1.keys()):
  22. q.write('{}: {}\n'.format(key, '; '.join(dict1.get(key))))
  23.  
  24. print('{}: {}'.format(key, '; '.join(dict1.get(key))))
  25.  
  26.  
  27. def output_titles(dict1, filename):
  28. titles = []
  29.  
  30. for title in dict1.values():
  31. titles.extend(title)
  32.  
  33. with open(filename, 'w+') as outfile:
  34.  
  35. for title in sorted(titles):
  36. outfile.write('{}\n'.format(title))
  37. print(title)
  38.  
  39.  
  40. def main(x):
  41. file_name = x
  42. dict1 = read_da_file(file_name)
  43. if dict1 is None:
  44. print('Error: Invalid file name provided: {}'.format(file_name))
  45.  
  46. return
  47. output_filename_1 = 'output_keys.txt'
  48. output_filename_2 = 'output_titles.txt'
  49. output_keys(dict1, output_filename_1)
  50. output_titles(dict1, output_filename_2)
  51.  
  52.  
  53. user_input = input()
  54. main(user_input)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement