import json from random import randint from datetime import datetime def test_json(): str_json = """ { "response": { "count": 5961878, "items": [{ "first_name": "Елизавета", "id": 620471795, "last_name": "Сопова", "can_access_closed": true }, { "first_name": "Роман", "id": 614752515, "last_name": "Малышев", "can_access_closed": true }] } } """ data = json.loads(str_json) # print(data['response']['items'][0]['id']) for item in data['response']['items']: del item['id'] item['likes'] = randint(100, 200) item['a'] = None item['now'] = datetime.now().strftime('%d.%m.%y') print(data['response']['items']) # new_json = json.dumps(data, indent=2) # print(new_json) # with open('my.json', 'w') as file: # json.dump(data, file, indent=3) # with open('my.json', 'r') as file: # new_json = json.load(file) # print(new_json) def test_1(): my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26} json_string = json.dumps(my_dict) print(json_string) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def car_manager(): my_list = [] with open("manager_sales.json", 'r') as file: data = json.load(file) for manager in data: cnt = 0 for cars in manager['cars']: cnt += cars['price'] my_list.append({'first_name': manager['manager']['first_name'], 'last_name': manager['manager']['last_name'], 'sum_price': cnt}) # print(my_list) price_max = 0 price_index = 0 for index, value in enumerate(my_list): if value['sum_price'] > price_max: price_max = value['sum_price'] price_index = index print(my_list[price_index]['first_name'], my_list[price_index]['last_name'], my_list[price_index]['sum_price']) # car_manager() #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # {"id_group":37,"people":[{"gender":"Female","name":"Annabelle Hows","year":1916},{"gender":"Female","name":"Harri Ogger","year":2002},{"gender":"Male","name":"Grant Candish","year":1951},{"gender":"Male","name":"Hersh Ryle","year":2008},{"gender":"Male","name":"Inigo Oertzen","year":1935},{"gender":"Female","name":"Delilah Kneath","year":1999}]}, def people(): my_list = [] with open('group_people.json', 'r') as file: data = json.load(file) # for gr in data['people']: for gr in data: # print(gr['id_group']) cnt = 0 for people in gr['people']: # print(people) if people['gender'] == 'Female' and people['year'] > 1977: cnt += 1 my_list.append([gr['id_group'], cnt]) # print(my_list) my_list = sorted(my_list, key=lambda x: -x[1]) print(my_list[0][0], my_list[0][1]) # people()