In [2]:
from json import dump
from library_task_3 import read_json
       
battles_dict = {}

# Extract all the battle's name from file
battles_lst = [battle.rstrip('\n') for battle in open('Data/battle_names.txt')]

# Fill the battles
for battle in battles_lst:
    battles_dict[battle] = {}

# Extract the locations
battles_locations_dict = read_json('Data/battle_locations.json')

# Fill the Battles' locations
for battle in battles_locations_dict.keys():
    battles_dict[battle]['Location'] = str(battles_locations_dict[battle]).strip('[]\'"')

# Extract the Region
regions_dict = read_json('Data/region_locations.json')

# Fill the Region    
for region, locations in regions_dict.items():
    for battle in battles_dict:
        if battles_dict[battle]['Location'] in locations:
            battles_dict[battle]['Region'] = region

# Extract the Attacker king
kings_attacker_dict = read_json('Data/king_attacker_battles.json')

# Fill the King Attacker    
for king in kings_attacker_dict.keys():
    for battle in kings_attacker_dict[king]:
        battles_dict[battle]['Attacker King'] = king

# Extract the Defender king
kings_defender_dict = read_json('Data/king_defender_battles.json')

# Fill the Defender Attacker    
for king in kings_defender_dict.keys():
    for battle in kings_defender_dict[king]:
        battles_dict[battle]['Defender King'] = king
        
with open('battles.json', 'w') as fp:
    dump(battles_dict, fp)

In [4]:
battles_dict


Out[4]:
{"Battle at the Mummer's Ford": {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Robb Stark',
  'Location': "Mummer's Ford",
  'Region': 'The Riverlands'},
 'Battle of Castle Black': {'Attacker King': 'Stannis Baratheon',
  'Defender King': 'Mance Rayder',
  'Location': 'Castle Black',
  'Region': 'Beyond the Wall'},
 'Battle of Deepwood Motte': {'Attacker King': 'Balon/Euron Greyjoy',
  'Defender King': 'Robb Stark',
  'Location': 'Deepwood Motte',
  'Region': 'The North'},
 'Battle of Duskendale': {'Attacker King': 'Robb Stark',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Duskendale',
  'Region': 'The Crownlands'},
 'Battle of Moat Cailin': {'Attacker King': 'Balon/Euron Greyjoy',
  'Defender King': 'Robb Stark',
  'Location': 'Moat Cailin',
  'Region': 'The North'},
 'Battle of Oxcross': {'Attacker King': 'Robb Stark',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Oxcross',
  'Region': 'The Westerlands'},
 'Battle of Riverrun': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Robb Stark',
  'Location': 'Riverrun',
  'Region': 'The Riverlands'},
 "Battle of Torrhen's Square": {'Attacker King': 'Robb Stark',
  'Defender King': 'Balon/Euron Greyjoy',
  'Location': "Torrhen's Square",
  'Region': 'The North'},
 'Battle of Winterfell': {'Attacker King': 'Balon/Euron Greyjoy',
  'Defender King': 'Robb Stark',
  'Location': 'Winterfell',
  'Region': 'The North'},
 'Battle of the Blackwater': {'Attacker King': 'Stannis Baratheon',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': "King's Landing",
  'Region': 'The Crownlands'},
 'Battle of the Burning Septry': {'Location': 'nan'},
 'Battle of the Camps': {'Attacker King': 'Robb Stark',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Riverrun',
  'Region': 'The Riverlands'},
 'Battle of the Crag': {'Attacker King': 'Robb Stark',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Crag',
  'Region': 'The Westerlands'},
 'Battle of the Fords': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Robb Stark',
  'Location': 'Red Fork',
  'Region': 'The Riverlands'},
 'Battle of the Golden Tooth': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Robb Stark',
  'Location': 'Golden Tooth',
  'Region': 'The Westerlands'},
 'Battle of the Green Fork': {'Attacker King': 'Robb Stark',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Green Fork',
  'Region': 'The Riverlands'},
 'Battle of the Ruby Ford': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Robb Stark',
  'Location': 'Ruby Ford',
  'Region': 'The Riverlands'},
 'Battle of the Shield Islands': {'Attacker King': 'Balon/Euron Greyjoy',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Shield Islands',
  'Region': 'The Reach'},
 'Battle of the Stony Shore': {'Attacker King': 'Balon/Euron Greyjoy',
  'Defender King': 'Robb Stark',
  'Location': 'Stony Shore',
  'Region': 'The North'},
 'Battle of the Whispering Wood': {'Attacker King': 'Robb Stark',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Whispering Wood',
  'Region': 'The Riverlands'},
 'Fall of Moat Cailin': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Balon/Euron Greyjoy',
  'Location': 'Moat Cailin',
  'Region': 'The North'},
 'Invasion of Ryamsport, Vinetown, and Starfish Harbor': {'Attacker King': 'Balon/Euron Greyjoy',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Ryamsport, Vinetown, Starfish Harbor',
  'Region': 'The Reach'},
 'Retaking of Deepwood Motte': {'Attacker King': 'Stannis Baratheon',
  'Defender King': 'Balon/Euron Greyjoy',
  'Location': 'Deepwood Motte',
  'Region': 'The North'},
 'Retaking of Harrenhal': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Location': 'Harrenhal',
  'Region': 'The Riverlands'},
 'Sack of Darry': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Robb Stark',
  'Location': 'Darry',
  'Region': 'The Riverlands'},
 'Sack of Harrenhal': {'Attacker King': 'Robb Stark',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Harrenhal',
  'Region': 'The Riverlands'},
 'Sack of Saltpans': {'Location': 'Saltpans', 'Region': 'The Riverlands'},
 "Sack of Torrhen's Square": {'Attacker King': 'Balon/Euron Greyjoy',
  'Defender King': 'Balon/Euron Greyjoy',
  'Location': "Torrhen's Square",
  'Region': 'The North'},
 'Sack of Winterfell': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Robb Stark',
  'Location': 'Winterfell',
  'Region': 'The North'},
 "Second Seige of Storm's End": {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Stannis Baratheon',
  'Location': "Storm's End",
  'Region': 'The Stormlands'},
 'Siege of Darry': {'Attacker King': 'Robb Stark',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Darry',
  'Region': 'The Riverlands'},
 'Siege of Dragonstone': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Stannis Baratheon',
  'Location': 'Dragonstone',
  'Region': 'The Stormlands'},
 'Siege of Raventree': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Robb Stark',
  'Location': 'Raventree',
  'Region': 'The Riverlands'},
 'Siege of Riverrun': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Robb Stark',
  'Location': 'Riverrun',
  'Region': 'The Riverlands'},
 'Siege of Seagard': {'Attacker King': 'Robb Stark',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Seagard',
  'Region': 'The Riverlands'},
 "Siege of Storm's End": {'Attacker King': 'Stannis Baratheon',
  'Defender King': 'Renly Baratheon',
  'Location': "Storm's End",
  'Region': 'The Stormlands'},
 'Siege of Winterfell': {'Attacker King': 'Stannis Baratheon',
  'Defender King': 'Joffrey/Tommen Baratheon',
  'Location': 'Winterfell',
  'Region': 'The North'},
 'The Red Wedding': {'Attacker King': 'Joffrey/Tommen Baratheon',
  'Defender King': 'Robb Stark',
  'Location': 'The Twins',
  'Region': 'The Riverlands'}}

In [ ]: