在python中附加现有的json数据,并将更新后的文件写回到json

时间:2019-10-06 16:23:52

标签: python json

我有一个json文件,其中包含具有3个对象的users数组,这些对象是我在设置json文件时创建的。我想读取python脚本中的数据,然后再将新的用户对象保存在我的json文件中的users数组下。我尝试附加数据,但仅附加了字典,没有更新我的json文件。有人可以帮忙吗?

这是我的json文件:

    {
"data":[
 {      
     "name":"greetings",
     "question":["hey","hi","hello"],
     "patterns":["hi","hey","hello"]
 },
 {
    "name":"start",
    "question":["how are you","how was your day","how are you doing"],
    "patterns":["Am great","it was great","Great!!"]
 },
 {  
    "name":"name", 
    "question":["what is your name","name please?"],
    "patterns":["My name is Elle, How may I help you"]
 }, 
 {
    "name":"menu", 
    "question":["whats in the menu","Can you please provide me with the menu "],
    "patterns":[
        {
            "starters":["cheese balls","chicken corn soup","mozrella sticks","prawn crisps"],
            "maincourse":["bar-b-q pizza","chicken burger","chowmin"],
            "deserts":["chocolate cake","apple pie","cake alaska","strawaberry icecream"]
        }
    ]
 },
 {  
    "name":"choice",
    "question":[],
    "pattern":["here you go enjoy your meal"]
 }
],
"users":[
    {     
      "userid":1,
      "username":"faheel sattar",
      "preference":""     
    },
    {     
        "userid":1,
        "username":"faheel sattar",
        "preference":""     
    },
    {     
        "userid":1,
        "username":"faheel sattar",
        "preference":""     
    } 
]
    }

这是我的python脚本:

    import json
    import numpy as np
    with open('lab3/data.json') as f:
      data = json.load(f)
      print(data["users"])
    users={
     "userid":4,
     "username":"fahdsdsdl sattar",
     "preference":",sdsd"
    }
    data["users"].append(users)
    data.update(data)
    with open('data.json', 'w') as f: 
      json.dump(data["users"],f)
      print(data["users"]) 

我想要在json中输出的内容是刚刚添加到用户库中的最后一个用户对象,该对象带有“ userid”:4:

    {
    data":[
    {      
     "name":"greetings",
     "question":["hey","hi","hello"],
     "patterns":["hi","hey","hello"]
    },
    {
     "name":"start",
     "question":["how are you","how was your day","how are you doing"],
     "patterns":["Am great","it was great","Great!!"]
    },
    {  
     "name":"name", 
     "question":["what is your name","name please?"],
     "patterns":["My name is Elle, How may I help you"]
    }, 
    {
     "name":"menu", 
     "question":["whats in the menu","Can you please provide me with the 
     menu "],
    "patterns":[
        {
            "starters":["cheese balls","chicken corn soup","mozrella 
             sticks","prawn crisps"],
            "maincourse":["bar-b-q pizza","chicken burger","chowmin"],
            "deserts":["chocolate cake","apple pie","cake 
             alaska","strawaberry icecream"]
        }
    ]
 },
 {  
    "name":"choice",
    "question":[],
    "pattern":["here you go enjoy your meal"]
 }
],
"users":[
    {     
      "userid":1,
      "username":"faheel sattar",
      "preference":""     
    },
    {     
        "userid":1,
        "username":"faheel sattar",
        "preference":""     
    },
    {     
        "userid":1,
        "username":"faheel sattar",
        "preference":""     
    },
    {     
        "userid":4,
        "username":"fahdsdsdl sattar",
        "preference":",sdsd"     
    } 

]
    }

0 个答案:

没有答案