Guest User

Untitled

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