Guest User

Untitled

a guest
Jan 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. Ivanon = {'math':4, 'english':4, 'physic':4, 'chemistry':4, 'biology':4}
  2. Netfullin = {'math':5, 'english':4, 'physic':4, 'chemistry':5, 'biology':4}
  3. Pasternak = {'math':5, 'english':4, 'physic':3, 'chemistry':5, 'biology':4}
  4. Lenina = {'math':5, 'english':5, 'physic':5, 'chemistry':5, 'biology':5}
  5.  
  6. Ivanon = {'math':4, 'english':4, 'physic':4, 'chemistry':4, 'biology':4}
  7. Doe = {'math':5, 'english':5, 'physic':5, 'chemistry':5, 'biology':5}
  8. Mustermann = {'math':5, 'english':5, 'physic':5}
  9. Dvoechnikov = {'math':3, 'english':3, 'physic':3, 'chemistry':3, 'biology':3}
  10.  
  11. [{'Name1':{'subj1':note, 'subj2':note, ...},
  12. {'Name2':{'subj1':note, 'subj2':note, ...}]
  13.  
  14. import re
  15. import ast
  16.  
  17. def parse_line(line):
  18. name,rest = re.split('s*=s*', line)
  19. d = ast.literal_eval(rest)
  20. return {name:d}
  21.  
  22. filename = r'C:Temp.data774534.txt'
  23.  
  24. with open(filename) as f:
  25. data = [parse_line(line) for line in f]
  26.  
  27. #best_ones = [n for x in data for n,d in x.items() if all(val == 5 for val in d.values())]
  28. best_ones = [n for x in data for n,d in x.items() if sum(d.values()) == 5*len(d)]
  29. print(best_ones)
  30.  
  31. ['Doe', 'Mustermann']
  32.  
  33. with open(input_file, 'r') as file:
  34. for line in file:
  35. if all([raiting[-1] == '5' for raiting in line[line.index('{') + 1:line.index('}')].split(', ')]):
  36. print(line[:line.index('=')].strip())
  37.  
  38. surname_dict = {surname: eval(_dict) for (surname, _dict) in (
  39. line.split('=', 1) for line in filter(str.strip, open('file.txt')))}
  40.  
  41. import json
  42. print(json.dumps(surname_dict, indent=4, sort_keys=True))
  43. # {
  44. # "Ivanon ": {
  45. # "biology": 4,
  46. # "chemistry": 4,
  47. # "english": 4,
  48. # "math": 4,
  49. # "physic": 4
  50. # },
  51. # "Lenina ": {
  52. # "biology": 5,
  53. # "chemistry": 5,
  54. # ...
  55. # }
  56. # }
  57.  
  58. for surname, subjects in surname_dict.items():
  59. if all((val == 5) for val in subjects.values()):
  60. print(surname, json.dumps(subjects, indent=4))
  61. # Lenina
  62. # {
  63. # "math": 5,
  64. # "english": 5,
  65. # "physic": 5,
  66. # "chemistry": 5,
  67. # "biology": 5
  68. # }
Add Comment
Please, Sign In to add comment