Convert a python dict to a string and back

后端 未结 10 1629
清酒与你
清酒与你 2020-11-27 10:17

I am writing a program that stores data in a dictionary object, but this data needs to be saved at some point during the program execution and loaded back into the dictionar

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 11:07

    Convert dictionary into JSON (string)

    import json 
    
    mydict = { "name" : "Don", 
              "surname" : "Mandol", 
              "age" : 43} 
    
    result = json.dumps(mydict)
    
    print(result[0:20])
    

    will get you:

    {"name": "Don", "sur

    Convert string into dictionary

    back_to_mydict = json.loads(result) 
    

提交回复
热议问题