简体   繁体   中英

How can in modify value of dict in dict in Python?

So i have json which looks like:

{
  "Name": "test",
  "CreationDate": "2021-12-1",
  "Commands": [
    {
      "Command": "open",
      "Target": "Link To go ",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "type",
      "Target": "name=emailOrPhone",
      "Value": "someEMAIL",
      "Targets": [
        "name=emailOrPhone",
        "xpath=//*[@id=\"react-root\"]/section/main/div/div/div/div/form/div[3]/div/label/input",
        "xpath=//input[@name='emailOrPhone']",
        "xpath=//input",
        "css=#react-root > section > main > div > div > div:nth-child(1) > div > form > div:nth-child(4) > div > label > input"
      ],
      "Description": ""
    },
    {
      "Command": "type",
      "Target": "name=fullName",
      "Value": "NAme Last Name ",
      "Targets": [
        "name=fullName",
        "xpath=//*[@id=\"react-root\"]/section/main/div/div/div/div/form/div[4]/div/label/input",
        "xpath=//input[@name='fullName']",
        "xpath=//div[4]/div/label/input",
        "css=#react-root > section > main > div > div > div:nth-child(1) > div > form > div:nth-child(5) > div > label > input"
      ],
      "Description": ""
    },
    {
      "Command": "type",
      "Target": "name=username",
      "Value": "USERNAME_HERE",
      "Targets": [
        "name=username",
        "xpath=//*[@id=\"react-root\"]/section/main/div/div/div/div/form/div[5]/div/label/input",
        "xpath=//input[@name='username']",
        "xpath=//div[5]/div/label/input",
        "css=#react-root > section > main > div > div > div:nth-child(1) > div > form > div:nth-child(6) > div > label > input"
      ],
      "Description": ""
    },
    {
      "Command": "type",
      "Target": "name=password",
      "Value": "PASS_HERE",
      "Targets": [
        "name=password",
        "xpath=//*[@id=\"react-root\"]/section/main/div/div/div/div/form/div[6]/div/label/input",
        "xpath=//input[@name='password']",
        "xpath=//div[6]/div/label/input",
        "css=#react-root > section > main > div > div > div:nth-child(1) > div > form > div:nth-child(7) > div > label > input"
      ],
      "Description": ""
    }
  ]
}

So how to edit values of email,username and passowrd ones? Do i have to iterate over each Command dict? if so,how to find 'Command' that have email field for example? Because these dictionaries can be mixed and do not always follow each other

Problem solved.

Here is the solution. Just need to iterate over each Command Dict.

for each in data["Commands"]:
    if each["Target"] == 'name=fullName' :
        each['Value'] = 'New Value'  # just updating the required value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM