Pull a dictionary out from a csv file in to a dictionary

this is my csv file

{'Name': 'The Hero', 'Min Damage': 2, 'Max Damage': 4, 'Defence': 1, 'HP': 20, 'Inventory': [], 'fought': False, 'Y': 1, 'X': 0}

this is what i have tried

def resumegame():
    filename = 'C:/test/savegame.csv'
    if not os.path.isfile(filename):
        return {}
    with open(filename) as ifh:
        return dict(line.split() for line in ifh)

You can use eval for your example, but it will not work for nested dictionaries (without more code).

with open(filename) as file:
    x = [line for line in file][0]
    dict = eval(x)

Or in the way you wrote it originally

def resumegame():
    with open(filename) as ifh:
        return eval([line for line in ifh][0])

dict = resumegame()
1 Like