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.
.pop(weapon)will fail asweaponisn't anint. I think you meant.remove(weapon)tkintertag from the question as the question isn't directly linked totkinter.