I wish to save a dictionary and a list in one json file so that when I parse the file I could distinguish them. This is the function I use to save the dictionary in the json file:
def save_callback():
#folder = filedialog.askdirectory()
file = filedialog.asksaveasfilename()
with open(file,'w') as file:
file.write(json.dumps(test_dict))
os.rename(file.name,file.name ".txt")
What should I add to this function to save the list too?
CodePudding user response:
Since JSON objects can be arbitrarily nested, the easiest solution is to store both the dict and the list inside another object:
data = json.dumps([test_dict, test_list])
Then you can load it like this:
test_dict, test_list = json.loads(data)