In the python3
code below I read all the objects
in the json
then loop through the elements
in the json
. If I only search for the id is not None
that element and the null value are removed. However when I add the or
statement to remove the id is not ""
the entire file is printed with neither of the id
in the or statement removed. Being new to python
I am not sure what I am doing wrong. Thank you :).
file
{
"objects": [
{
"version": "1",
"list": [
{
"
"json": null
},
"id": "",
}
],
"h": "na"
},
{
"version": "1",
"list": [
{
"
"json": null
},
"id": "This has text in it and should be printed",
}
],
"h": "na"
},
{
"version": "1",
"list": [
{
"
"json": null
},
"id": null,
}
],
"h": "na"
}
]
}
desired
{
"version": "1",
"list": [
{
"
"json": null
},
"id": "This has text in it and should be printed",
}
],
"h": "na"
},
python3
if v["id"] is not None or v['id'] is not "":
Also tried the below and id
were changed:
for obj in data['objects']:
if obj.get('id') is "":
obj['description'] = 'na'
CodePudding user response:
if v["id"] is not None or v['id'] is not "":
No matter what the value is, this statement will always be true.
If the value is None, then the is not ""
part will be true.
If the value is an empty string, then the is not None
part will be true.
And if the value is anything else, then both parts will be true.
As @Pranav commented, you want to use and
here, not or
.