Здравствуйте.
Стоит задача извлечения данных из json файла. Его кусок выглядит так:
'camps_stacked': 0,
'creeps_stacked': 0,
'damage': {'npc_dota_badguys_siege': 291,
'npc_dota_badguys_tower1_mid': 46,
'npc_dota_creep_badguys_melee': 3599,
'npc_dota_creep_badguys_ranged': 1677,
'npc_dota_hero_bounty_hunter': 1709,
'npc_dota_hero_dragon_knight': 2809,
'npc_dota_hero_ember_spirit': 1463,
'npc_dota_hero_phoenix': 3557,
'npc_dota_hero_troll_warlord': 1678,
'npc_dota_observer_wards': 8},
Для парсинга написал написал такой скрипт:
import collections
MATCH_FEATURES = [
('game_time', lambda m: m['game_time'])
]
PLAYER_FIELDS = [
'hero_id',
'kills',
'creeps_stacked'
]
def extract_features_csv(match):
row = [
('match_id_hash', match['match_id_hash']),
]
for field, f in MATCH_FEATURES:
row.append((field, f(match)))
for slot, player in enumerate(match['players']):
if slot < 5:
player_name = 'r%d' % (slot + 1)
else:
player_name = 'd%d' % (slot - 4)
for field in PLAYER_FIELDS:
column_name = '%s_%s' % (player_name, field)
row.append((column_name, player[field]))
return collections.OrderedDict(row)
def extract_targets_csv(match, targets):
return collections.OrderedDict([('match_id_hash', match['match_id_hash'])] + [
(field, targets[field])
for field in ['game_time', 'radiant_win', 'duration', 'time_remaining', 'next_roshan_team']
])
Но не пойму, как в приведенном примере части json файла получить, например, сумму всех значений в поле "damage"?