How to reformat data into actual sentences using python?

57 views Asked by At

I have text from a JSON file in this format:

"Abomasnow": {"level": 86, "abilities": ["Snow Warning"], "items": ["Assault Vest", "Light Clay"], "roles": {"Bulky Support": {"abilities": ["Snow Warning"], "items": ["Assault Vest", "Light Clay"], "teraTypes": ["Ice", "Water"], "moves": ["Aurora Veil", "Blizzard", "Earthquake", "Ice Shard", "Wood Hammer"]}}},
 
"Alomomola": {"level": 87, "abilities": ["Regenerator"], "items": ["Leftovers", "Rocky Helmet"], "roles": {"Bulky Support": {"abilities": ["Regenerator"], "items": ["Leftovers", "Rocky Helmet"], "teraTypes": ["Steel"], "moves": ["Body Slam", "Liquidation", "Mirror Coat", "Protect", "Wish"]}}},
 
"Altaria": {"level": 88, "abilities": ["Natural Cure"], "items": ["Heavy-Duty Boots", "Leftovers"], "roles": {"Bulky Support": {"abilities": ["Natural Cure"], "items": ["Heavy-Duty Boots"], "teraTypes": ["Steel"], "moves": ["Brave Bird", "Defog", "Earthquake", "Haze", "Roost", "Will-O-Wisp"]}, "Bulky Setup": {"abilities": ["Natural Cure"], "items": ["Heavy-Duty Boots", "Leftovers"], "teraTypes": ["Flying", "Ground"], "moves": ["Brave Bird", "Dragon Dance", "Earthquake", "Roost"]}}},

There are hundreds of rows. I want to format this so it is more readable. Is there any way to do this with Python so it is not manual. Which will involve looping through each row, some of which are more complicated than others.

The output I want would be something like the following:

Abomasnow has the ability snow warning, can hold items assault vest, light clay, has the tera types ice or water, learns the moves Aurora Veil, Blizzard, Earthquake, Ice Shard, Wood Hammer.

Or could just be

Abomasnow, abilities snow warning, items assault vest or light clay, tera type ice or water, moves Aurora Veil, Blizzard, Earthquake, Ice Shard, Wood Hammer

Would this be at all possible, or would it be best just to remove all the brackets?

I have tried removing the brackets but still looks a bit bad. Not too sure how to proceed

1

There are 1 answers

5
Stef On

You can write a function pkmn_verbose_str to build a string using the data in the dictionary.

def list_str(data, keyword, singular, plural):
    if keyword not in data:
        return ' '.join(('no', singular))
    n = len(data[keyword])
    if n == 0:
        return ' '.join(('no', singular))
    elif n == 1:
        return ' '.join((singular, data[keyword][0]))
    else:
        return ' '.join((plural, ', '.join(data[keyword])))

def pkmn_verbose_str(pkmn_name, data):
    abilities = list_str(data, 'abilities', 'ability', 'abilities')
    items = list_str(data, 'items', 'item', 'items')
    teratypes = list_str(data['roles']['Bulky Support'], 'teraTypes', 'tera type', 'tera types')
    moves = list_str(data['roles']['Bulky Support'], 'moves', 'move', 'moves')
    return f'{pkmn_name} has the {abilities}, can hold {items}, has the {teratypes}, learns the {moves}.'

d = {"Abomasnow": {"level": 86, "abilities": ["Snow Warning"], "items": ["Assault Vest", "Light Clay"], "roles": {"Bulky Support": {"abilities": ["Snow Warning"], "items": ["Assault Vest", "Light Clay"], "teraTypes": ["Ice", "Water"], "moves": ["Aurora Veil", "Blizzard", "Earthquake", "Ice Shard", "Wood Hammer"]}}}, "Alomomola": {"level": 87, "abilities": ["Regenerator"], "items": ["Leftovers", "Rocky Helmet"], "roles": {"Bulky Support": {"abilities": ["Regenerator"], "items": ["Leftovers", "Rocky Helmet"], "teraTypes": ["Steel"], "moves": ["Body Slam", "Liquidation", "Mirror Coat", "Protect", "Wish"]}}}, "Altaria": {"level": 88, "abilities": ["Natural Cure"], "items": ["Heavy-Duty Boots", "Leftovers"], "roles": {"Bulky Support": {"abilities": ["Natural Cure"], "items": ["Heavy-Duty Boots"], "teraTypes": ["Steel"], "moves": ["Brave Bird", "Defog", "Earthquake", "Haze", "Roost", "Will-O-Wisp"]}, "Bulky Setup": {"abilities": ["Natural Cure"], "items": ["Heavy-Duty Boots", "Leftovers"], "teraTypes": ["Flying", "Ground"], "moves": ["Brave Bird", "Dragon Dance", "Earthquake", "Roost"]}}},}

lines = [pkmn_verbose_str(k, v) for k,v in d.items()]

print(*lines, sep='\n')
# Abomasnow has the ability Snow Warning, can hold items Assault Vest, Light Clay, has the tera types Ice, Water, learns the moves Aurora Veil, Blizzard, Earthquake, Ice Shard, Wood Hammer.
# Alomomola has the ability Regenerator, can hold items Leftovers, Rocky Helmet, has the tera type Steel, learns the moves Body Slam, Liquidation, Mirror Coat, Protect, Wish.
# Altaria has the ability Natural Cure, can hold items Heavy-Duty Boots, Leftovers, has the tera type Steel, learns the moves Brave Bird, Defog, Earthquake, Haze, Roost, Will-O-Wisp.

Then use module json from standard library to read the json file into a python dict, apply pkmn_verbose_str to the items in that dict, and then write the resulting list of lines to a new text file.

Or perhaps better suited to the structure of your data:

def pkmn_verbose_str(pkmn_name, data):
    abilities = list_str(data, 'abilities', 'ability', 'abilities')
    items = list_str(data, 'items', 'item', 'items')
    if 'roles' not in data or len(data['roles']) == 0:
        return f'{pkmn_name} (no role) has the {abilities}, can hold {items}.'
    else:
        result_list = []
        for role_name, subdata in data['roles'].items():
            teratypes = list_str(subdata, 'teraTypes', 'tera type', 'tera types')
            moves = list_str(subdata, 'moves', 'move', 'moves')
            result_list.append(f'{pkmn_name} {role_name} has the {abilities}, can hold {items}, has the {teratypes}, learns the {moves}.')
        return '\n'.join(result_list)

lines = [pkmn_verbose_str(k, v) for k,v in d.items()]

print(*lines, sep='\n')

# Abomasnow Bulky Support has the ability Snow Warning, can hold items Assault Vest, Light Clay, has the tera types Ice, Water, learns the moves Aurora Veil, Blizzard, Earthquake, Ice Shard, Wood Hammer.
# Alomomola Bulky Support has the ability Regenerator, can hold items Leftovers, Rocky Helmet, has the tera type Steel, learns the moves Body Slam, Liquidation, Mirror Coat, Protect, Wish.
# Altaria Bulky Support has the ability Natural Cure, can hold items Heavy-Duty Boots, Leftovers, has the tera type Steel, learns the moves Brave Bird, Defog, Earthquake, Haze, Roost, Will-O-Wisp.
# Altaria Bulky Setup has the ability Natural Cure, can hold items Heavy-Duty Boots, Leftovers, has the tera types Flying, Ground, learns the moves Brave Bird, Dragon Dance, Earthquake, Roost.