如何使用python根据JSON文件中的条件修改属性的值

问题描述 投票:0回答:1

我对JSON文件一无所知,我正在尝试在python中使用带有if结构的循环来修改其中的一些值。

我认为这将是一项简单的任务,但由于我对 JSON 文件一无所知,我在访问启动条件的值时遇到问题。

Json 文件:

{
    "dwellers": [
        {
            "serializeId": 1,
            "name": "Kathleen",
            "lastName": "Hall",
            "gender": 1,
            "pregnant": false,
            "babyReady": false,
            "equipedOutfit": {
                "id": "jumpsuit",
                "type": "Outfit",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            },
            "equipedWeapon": {
                "id": "Fist",
                "type": "Weapon",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            }
        },

        {
            "serializeId": 2,
            "name": "MS",
            "lastName": "1",
            "gender": 2,
            "pregnant": false,
            "equipedOutfit": {
                "id": "JobinsonsJersey",
                "type": "Outfit",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            },
            "equipedWeapon": {
                "id": "PlasmaThrower_DragonsMaw",
                "type": "Weapon",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            }
        },

        {
            "serializeId": 3,
            "name": "WSLD",
            "lastName": "1",
            "gender": 2,
            "pregnant": false,
            "babyReady": false,
            "equipedOutfit": {
                "id": "JobinsonsJersey",
                "type": "Outfit",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            },
            "equipedWeapon": {
                "id": "PlasmaThrower_DragonsMaw",
                "type": "Weapon",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            }
        }
    ]
}

“性别”属性将其值更改为1或2,其中1为女性,2为男性。 如果“性别”为1,我想把“怀孕”属性从false改为true,false表示未怀孕,true表示怀孕。

功能:

def gimmeChild():
    with open('./mindweller.json', 'r') as file:
        dwellers = json.load(file)
    
    for object in dwellers:
        if object["pregnant"] == "false":
            object["pregnant"] == "true"
            break
        else:
            print("erro")
            
    dwellers.close()

我还想修改“equipedOutfit”和“equipedWeapon”属性。 它们对应于角色所装备的服装和武器,根据每种武器的ID而变化。

如果“equipedOutfit”的 id 与“JobinsonsJersey”不同,我希望它成为“JobinsonsJersey”。

功能:

def gimmeOutfit():
    with open('./mindweller.json', 'r') as file:
        dwellers = json.load(file)
        
    for object in dwellers:
        if object["equipedOutfit"]["id"] != "JobinsonsJersey":
            object["equipedOutfit"]["id"] = "JobinsonsJersey"
            break
        else:
            print("erro")
            
    dwellers.close()

如果“equipedWeapon”的 id 与“PlasmaThrower_DragonsMaw”不同,我希望它变成“PlasmaThrower_DragonsMaw”

功能:

def gimmeGun():
    with open('./mindweller.json', 'r') as file:
        dwellers = json.load(file)
    
    for object in dwellers:
        if object["equipedWeapon"]["id"] != "PlasmaThrower_DragonsMaw":
            object["equipedWeapon"]["id"] = "PlasmaThrower_DragonsMaw"
            break
        else:
            print("erro")
            
    dwellers.close()

对于所有函数,我都会收到回溯错误,因为我真的不知道如何访问属性。

有人可以帮助我吗!?

python json loops if-statement traceback
1个回答
0
投票

您可以使用此示例如何将 Json 数据加载到 Python 对象并更改各种属性:

import json

with open("data.json", "r") as f_in:
    data = json.load(f_in)

for d in data["dwellers"]:
    # 1. change to "pregnant"
    if d["gender"] == 1:
        d["pregnant"] = True

    # 2. equipedOutfit -> JobinsonsJersey
    d["equipedOutfit"]["id"] = "JobinsonsJersey"

    # 3. equipedWeapon -> PlasmaThrower_DragonsMaw
    d["equipedWeapon"]["id"] = "PlasmaThrower_DragonsMaw"

with open("data_out.json", "w") as f_out:
    json.dump(data, f_out, indent=4)

创建这个

data_out.json

{
    "dwellers": [
        {
            "serializeId": 1,
            "name": "Kathleen",
            "lastName": "Hall",
            "gender": 1,
            "pregnant": true,
            "babyReady": false,
            "equipedOutfit": {
                "id": "JobinsonsJersey",
                "type": "Outfit",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            },
            "equipedWeapon": {
                "id": "PlasmaThrower_DragonsMaw",
                "type": "Weapon",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            }
        },
        {
            "serializeId": 2,
            "name": "MS",
            "lastName": "1",
            "gender": 2,
            "pregnant": false,
            "equipedOutfit": {
                "id": "JobinsonsJersey",
                "type": "Outfit",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            },
            "equipedWeapon": {
                "id": "PlasmaThrower_DragonsMaw",
                "type": "Weapon",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            }
        },
        {
            "serializeId": 3,
            "name": "WSLD",
            "lastName": "1",
            "gender": 2,
            "pregnant": false,
            "babyReady": false,
            "equipedOutfit": {
                "id": "JobinsonsJersey",
                "type": "Outfit",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            },
            "equipedWeapon": {
                "id": "PlasmaThrower_DragonsMaw",
                "type": "Weapon",
                "hasBeenAssigned": false,
                "hasRandonWeaponBeenAssigned": false
            }
        }
    ]
}
© www.soinside.com 2019 - 2024. All rights reserved.