我有一個包含用戶級別和經驗的簡單 json 文件,格式如下:{ "users" : { "1" : [0, 1], "2" : [10, 2], }}該users對象使用用戶 ID 作為[xp, level]數組值的鍵。我需要能夠將新用戶寫入該文件,以便以后我可以將其稱為data["users"][id].到目前為止,這是我的代碼,但它實際上并沒有寫入文件。import jsondef create_user(id): with open("test.json") as file: data = json.load(file) temp = data["users"] # get the users object. temp[str(id)] = [0, 0] # [0, 0] would be the default, until the user # gains levels and xp. print('user added to file.')我該怎么做才能將用戶添加到users對象并保存到文件中?謝謝。
使用 python 將項目添加到 json 對象
慕尼黑8549860
2023-01-04 16:10:45