[Python] Writing to a Dictionary permanently

Discussão em 'Python' iniciado por Stack, Fevereiro 4, 2025.

  1. Stack

    Stack Membro Participativo

    Firstly Im not sure if this is even the best approach.

    So im trying to figure out how to write to a dictionary so that the new keys and values are stored even after I terminate the program. So lets say I have:

    mydict = {'Name':'John', 'Favourite Colour': 'Blue', 'Age':'10'}
    print(mydict)

    mydict['Food'] = 'Chicken'

    print(mydict)


    Output:

    {'Name': 'John', 'Favourite Colour': 'Blue', 'Age': '10'}
    {'Name': 'John', 'Favourite Colour': 'Blue', 'Age': '10', 'Food': 'Chicken'}


    The next time I launch this program and the dict['Food'] = 'Chicken'was removed. I would like food key to already be stored there. After that I would like to create another dictionary for lets say 'Bob' with these keys but give different values based on user input.

    Should I start looking at making a database or is a dictionary a way of going about this?

    Continue reading...

Compartilhe esta Página