如何在python中添加到json文件

如何在python中添加到json文件,python,json,Python,Json,我可以读取json文件并设置变量所需的信息。我有账户,我需要能够向其中添加一个账户,这是我目前拥有的代码: import json user = raw_input("User: ") with open('text.json') as data_file: data = json.load(data_file) code = data["users"][user]["pass"] display = data["users"][user]["display"] print "

我可以读取json文件并设置变量所需的信息。我有账户,我需要能够向其中添加一个账户,这是我目前拥有的代码:

import json

user = raw_input("User: ")

with open('text.json') as data_file:
    data = json.load(data_file)

code =  data["users"][user]["pass"]
display = data["users"][user]["display"]

print "Password: " + code
print "Display Name - " + display
这可以很好地读取文件,以下是输出json文件的预期格式:

{
  "users":{
    "User1": {
      "display": "User1",
      "pass": "EzPass123"
    },
    "Richard": {
      "display": "Attack69",
      "pass": "Smith"
    }
  }
}

有人能告诉我如何将另一个帐户添加到字典中已有的帐户中吗?

这里有一个解决方案,涵盖了另一个答案中讨论的修复:

import json

# here you read your new user and his password
user = raw_input("User: ")
display = raw_input("Display name: ")
pwd = raw_input("Pass: ")

with open('text.json') as data_file:
    data = json.load(data_file)
    # update the data dictionary then re-dump it in the file
    data['users'].update({
        user: {
            'display': display,
            'pass': pwd
        }
    })

with open('text.json', 'w') as data_file:
    json.dumps(data, data_file, indent=4)
#!python2
import json

# here you read your new user and his password
user = raw_input("User: ")
display = raw_input("Display name: ")
pwd = raw_input("Pass: ")

with open('text.json') as data_file:
    data = json.load(data_file)
    data['users'][user] = {'display':display,'pass':pwd}

with open('text.json','w') as data_file:
    json.dump(data, data_file, indent=4)
输出:

User: markt
Display name: Mark
Pass: 123
结果:

{
    "users": {
        "markt": {
            "display": "Mark",
            "pass": "123"
        },
        "Richard": {
            "display": "Attack69",
            "pass": "Smith"
        },
        "User1": {
            "display": "User1",
            "pass": "EzPass123"
        }
    }
}

下面是一个解决方案,涵盖了另一个答案中讨论的修复:

#!python2
import json

# here you read your new user and his password
user = raw_input("User: ")
display = raw_input("Display name: ")
pwd = raw_input("Pass: ")

with open('text.json') as data_file:
    data = json.load(data_file)
    data['users'][user] = {'display':display,'pass':pwd}

with open('text.json','w') as data_file:
    json.dump(data, data_file, indent=4)
输出:

User: markt
Display name: Mark
Pass: 123
结果:

{
    "users": {
        "markt": {
            "display": "Mark",
            "pass": "123"
        },
        "Richard": {
            "display": "Attack69",
            "pass": "Smith"
        },
        "User1": {
            "display": "User1",
            "pass": "EzPass123"
        }
    }
}


举一个例子,你的结果应该像我在json文件中做的那样,它应该像那样吗?我不熟悉json术语,我的代码来自另一个问题,我只是修改了它。你能解释得更详细一点吗?举个例子,你的结果在json文件中应该是怎样的,它应该是这样的?我不熟悉json术语,我的代码来自另一个问题,我只是修改了它。你能解释得更详细一点吗?这将用这些信息替换user1,我需要添加一个新的userwhops,打字,现在更新,由于某种原因不会添加另一个帐户,但我没有错误
json.dump
,而不是
json.dumps
。如果使用相同的开放实例来读写.Traceback(最后一次调用):json.dump(data,data_file)文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/_uinit_uu.py)第190行dump fp.write(chunk)中的文件“add.py”,可能也需要倒带文件这将用这些信息替换user1,我需要添加一个新的userwhops,键入,立即更新,由于某种原因不会添加其他帐户,但我没有错误
json.dump
,而不是
json.dumps
。如果使用相同的开放实例进行读写,可能也需要倒带文件。Traceback(最后一次调用):json.dump(data,data_file)文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/_uinit_uuu.py中的文件“add.py”,第17行,dump fp.write(chunk)Hey中的第190行,我让它成为it用户的用户名,密码=数据[“用户”][用户][“通过”]当创建一个新帐户时,我会检查是否有一个帐户使用他们输入的名称,如果他们输入的名称不在数据库中,它就会断开,有没有办法解决这个问题?有没有办法将所有名称设置为一个变量,例如here@RichardSmith,你应该问一个新问题,显示对现有用户的附加代码检查。
data['users'].keys()
将为你提供姓名列表。嘿,我在它的用户处设置用户名,密码=data['users][[user][“pass”]创建新帐户时,我会检查是否有他们输入的姓名的帐户,如果他们输入一个不在数据库中的名称,它就会中断,有没有办法解决这个问题?有没有办法将所有名称设置为一个变量,例如here@RichardSmith,您应该提出一个新问题,显示现有用户的附加代码检查。
data['users'].keys()
将为您提供名称列表。