蟒蛇/休息。检查条目是否已存在并执行某些操作

时间:2015-08-26 11:27:07

标签: python rest for-loop python-requests

我是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中不存在时才添加项目。因此,我需要检查“名称”字段,如下所示:

   [ {
  "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"

1 个答案:

答案 0 :(得分:0)

for entry in j: if 'xyz' not in str(entry): print(entry)