How could I save data after closing my program?

后端 未结 3 783
无人共我
无人共我 2021-01-07 07:52

I am currently working on a phone book directory using dictionaries. I didn\'t know any way to save the information after closing the program. I need to save the variable In

3条回答
  •  一整个雨季
    2021-01-07 08:40

    You can write to a text file as a string, then read parsing as a dictionary:

    Write

    with open('info.txt', 'w') as f:
        f.write(str(your_dict))
    

    Read

    import ast
    
    with open('info.txt', 'r') as f:
        your_dict = ast.literal_eval(f.read())
    

提交回复
热议问题