【发布时间】:2020-06-10 03:46:16
【问题描述】:
我有一个包含用户级别和体验的简单 json 文件,格式如下:
{
"users" : {
"1" : [0, 1],
"2" : [10, 2],
}
}
users 对象使用用户 ID 作为 [xp, level] 数组值的键。我需要能够将新用户写入文件,以便稍后我可以将其称为data["users"][id]。
到目前为止,这是我的代码,但它实际上并没有写入文件。
import json
def 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 arrays json python-3.x database