Home > Mobile >  Remove item / data from Json file with Python
Remove item / data from Json file with Python

Time:09-16

I have the following .json file named weapons.json:

{
  "weapons": [
    {
      "game": "EMPTY",
      "weapon": "EMPTY",
      "down": "0",
      "up": "0",
      "left": "0",
      "right": "0"
    },
    {
      "game": "DELETE",
      "weapon": "DELETE",
      "down": "0",
      "up": "0",
      "left": "0",
      "right": "0"
    }
  ]
}

This data is only for testing.

From this file I want to remove the second item :

{
  "game": "DELETE",
  "weapon": "DELETE",
  "down": "0",
  "up": "0",
  "left": "0",
  "right": "0"
}

For this, I have tried the following function:

#Deleting json info from the selected weapon
def deleteWeapon():
        with open('json/weapons.json') as info:
                weapons_dict =  json.load(info)
        
        for weapon in weapons_dict['weapons']:
                if (weapon['game']=="DELETE" and weapon['weapon']=="DELETE" and weapon['down']=="0" and weapon['up']=="0" and weapon['left']=="0" and weapon['right']=="0"):
                        del weapon
                        
        with open('json/weapons.json','w') as remove:
                json.dump(weapons_dict,remove,indent=2)
                
deleteWeapon()

Where I try to do a del weapon to remove the value, but this does not work. I have also tried doing del weapon['XXX'] del weapon['XXX'] with all item values, but doing this the .json looks like this.

{
  "weapons": [
    {
      "game": "EMPTY",
      "weapon": "EMPTY",
      "down": "0",
      "up": "0",
      "left": "0",
      "right": "0"
    },
    {}
  ]
}

Instead of :

{
  "weapons": [
    {
      "game": "EMPTY",
      "weapon": "EMPTY",
      "down": "0",
      "up": "0",
      "left": "0",
      "right": "0"
    }
  ]
}

I am grateful to hear any help.

CodePudding user response:

Already found the solution.

The code should look like this :

def deleteWeapon():
        with open('json/weapons.json') as info:
                weapons_dict =  json.load(info)
        
        weapons = list(weapons_dict["weapons"])
        for i, weapon in enumerate(weapons):
                if (weapon['game']=="DELETE" and weapon['weapon']=="DELETE" and weapon['down']=="0" and weapon['up']=="0" and weapon['left']=="0" and weapon['right']=="0"):
                        weapons_dict['weapons'].pop(i)
                        
        with open('json/weapons.json','w') as remove:
                json.dump(weapons_dict,remove,indent=2)

CodePudding user response:

Try this:

weapons = list(weapons_dict["weapons"])
for i, weapon in enumerate(weapons):
    if (weapon['game']=="DELETE" and weapon['weapon']=="DELETE" and weapon['down']=="0" and weapon['up']=="0" and weapon['left']=="0" and weapon['right']=="0"):
        del weapons_dict["weapons"][i]

That iterate over the list using i as the current index and then it will call del weapons_dict["weapons"][i] when ever it needs to delete that weapon.

  • Related