比较和删除api数据

时间:2017-10-12 21:05:25

标签: python django

我有一个django项目,它从我们的另一个应用程序中获取一些数据。数据如下所示:

{u'   updated':   u'2017-04-03T22:30:53.760278   Z',
   u'added':   u'2017-04-03T22:30:53.760197   Z',
   u'name':u'Jean Lamb,
   1942-   ', u'   authority':{  
      u'id':2,
      u'added_by':2,
      u'name':u'VIAF'
   },
   u'local_identifier':u'85363862',
   u'concept_type':{  
      u'id':5,
      u'identifier':      u'viaf:personal',
      u'name':u'',
      u'description':None
   },
   u'identifier':   u'http://viaf.org/viaf/85363862',
   u'identities':[  
      {  
         u'part_of':{  
            u'id':1,
            u'added_by':2,
            u'name':            u'builtin:Conceptpower'
         },
         u'added':         u'2017-04-03T22:33:20.476637         Z',
         u'name':u'Jean Lamb',
         u'confidence':1.0,
         u'updated':         u'2017-04-03T22:33:20.476699         Z',
         u'concepts':[  
            u'http://viaf.org/viaf/85363862',
            u'http://www.digitalhps.org/concepts/CONpeSHC70qxNC0'
         ],
         u'id':208,
         u'added_by':{  
            u'username':u'erickjones',
            u'email':u'erick.jones@example.com'
         }
      },
      {  
         u'part_of':{  
            u'id':1,
            u'added_by':2,
            u'name':            u'builtin:Conceptpower'
         },
         u'added':         u'2017-04-03T22:35:02.546054         Z',
         u'name':u'Jean Lamb',
         u'confidence':1.0,
         u'updated':         u'2017-04-03T22:35:02.546116         Z',
         u'concepts':[  
            u'http://viaf.org/viaf/85363862',
            u'http://www.digitalhps.org/concepts/CONpeSHC70qxNC0'
         ],
         u'id':209,
         u'added_by':{  
            u'username':u'erickjones',
            u'email':u'erick.jones@example.com'
         }
      },

现在我有一个功能可以通过并比较身份中的概念。我想要做的是删除重复的概念。字典和列表的嵌套让我失望。我一直在尝试的是:

del results[i]["identities"][z]["concepts"]

关于为什么这不起作用的任何想法?

这是我的循环,任何人都有兴趣:

while (i != di):
    test = results[i]["identities"]
    if results[i]["identities"]:
        z = 0
        while (z != len(results[i]["identities"])):
            con1 = results[i]["identities"][z]["concepts"]
            print "this is con1: %s", con1
            if z != len(results[i]["identities"]):
                z = z + 1
            else:
                break
            if z != len(results[i]["identities"]):
                con2 = results[i]["identities"][z]["concepts"]
                print "this is con2: %s", con2
                if set(con1) == set(con2):


                   del results[i]["identities"][z]["concepts"]

            else:
                break


    i = i + 1

1 个答案:

答案 0 :(得分:1)

在这一行中,

    for image_url in largest_images:
        # SOME VARIABLES
        timeout = 20
        response = None
        file_name = md5(image_url)
        file_path = os.path.join(dst_dir, file_name)
        try_times = 0    

        while True:
            try:
                try_times += 1
                response = requests.get(image_url, headers=headers, timeout=timeout)
                if response.status_code == 200:
                    print "Downloading: %s" % image_url
                    with open(file_path, 'wb') as f:
                        for chunk in response.iter_content(1024):
                            f.write(chunk)
                    response.close()
                    file_type = imghdr.what(file_path)
                    # if file_type is not None:
                    if file_type in ["jpg", "jpeg", "png", "bmp"]:
                        new_file_name = "{}.{}".format(file_name,file_type)
                        new_file_path = os.path.join(dst_dir, new_file_name)
                        shutil.move(file_path, new_file_path)
                        print ("## OK: {} {}".format(new_file_name, image_url))
                        break (I have added a break statement here, but its not working)
                    else:
                        os.remove(file_path)
                        print ("## Err: {}".format(image_url))
                    break
            except Exception as e:
                if try_times < 3:
                    continue
                if response:
                    response.close()
                print ("## Fail: {} {}".format(image_url, e.args))
                break    

您是否打算检查con1和con2是否相同?你不应该使用==运算符吗?