Python/Rest。检查条目是否已经存在并做一些事情
Python/Rest. Check if entry already exists and do something
我是 python 的新手,想做以下事情:
我通过 requests.get 收到 json。我想遍历它并打印所有没有 'name' 的条目:'xyz'.
我愿意:
s = requests.get(instrItemList, verify=False, auth=auth)
j = s.json()
现在我想遍历 j 并说:只打印那些不是 'name' 的项目名称:'xyz'。类似于:
for entry in j:
...
print entry ['name']
谢谢。
亲切的问候
编辑:
我想说明我的问题:
我只想添加一个在 j 中不存在的项目。因此我需要检查 "name" 字段,它看起来像这样:
[ {
"uri" : "/item/12947",
"version" : 1,
"tracker" : {
"uri" : "/category/73718",
"name" : "Instructions"
},
"priority" : {
"id" : 2,
"name" : "High",
"flags" : 0
},
"name" : "Testitem",
"status" : {
"id" : 1,
"name" : "New",
"flags" : 0
我怎么能说:
if "Testitem" not in j: ... here i need to say that only the "name:" value needs to be checked....
print "Item does not exist and has been created"
else: print "Item already exists"
for entry in j:
if 'xyz' not in str(entry):
print(entry)
我是 python 的新手,想做以下事情:
我通过 requests.get 收到 json。我想遍历它并打印所有没有 'name' 的条目:'xyz'.
我愿意:
s = requests.get(instrItemList, verify=False, auth=auth)
j = s.json()
现在我想遍历 j 并说:只打印那些不是 'name' 的项目名称:'xyz'。类似于:
for entry in j:
...
print entry ['name']
谢谢。 亲切的问候
编辑:
我想说明我的问题:
我只想添加一个在 j 中不存在的项目。因此我需要检查 "name" 字段,它看起来像这样:
[ {
"uri" : "/item/12947",
"version" : 1,
"tracker" : {
"uri" : "/category/73718",
"name" : "Instructions"
},
"priority" : {
"id" : 2,
"name" : "High",
"flags" : 0
},
"name" : "Testitem",
"status" : {
"id" : 1,
"name" : "New",
"flags" : 0
我怎么能说:
if "Testitem" not in j: ... here i need to say that only the "name:" value needs to be checked....
print "Item does not exist and has been created"
else: print "Item already exists"
for entry in j:
if 'xyz' not in str(entry):
print(entry)