Guest User

Untitled

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