Home > Software engineering >  python recursive key and value find on condition
python recursive key and value find on condition

Time:06-18

I have written this function to find key @type of a specific value. I mean, I want to get all the objects who have key @type of specific value. the objects is very nested. and the key is @type is duplicate, it can be found many place, I guess that is why it is not working:

def recurseitem(obj, value):
    if '@type' in obj:
        if obj['@type'] in value:
            return obj
    for k, v in obj.items():
        if isinstance(v,dict):
            return recurseitem(v, value)

and processing data here

def get_results(data): # data = []
    results = {}

    valuetype_to_find = { 
        'product': ['product', 'Product', 'car', 'Car'],
        'AutoDealer': ['AutoDealer'],
        'PostalAddress': ['PostalAddress'],
        'offer': ['Offer', 'offer', 'Offer',]
    }
  
    for vk, vt in valuetype_to_find.items():
        results[vk] = recurseitem(data, vt) or {}

    return results

I am expecting get_results should return data like this:

{ 
    'product': {..all the product key from data.....}
    'AutoDealer': {...all the AutoDealer..data.....}
    'PostalAddress': {...all the postal address data.......}
    'offer': {...all the offer data.......}
}

But unfortunately it is not working, I am not sure what's wrong with this. it should work well but not working !

here you go for example data

data = {
   "@context":"https://schema.org",
   "@type":"Product",
   "additionalType":"http://www.pr.org/id/Car",
   "name":"2022 Honda Ridgeline RTL AWD",
   "description":"Sunroof, HPlease confirm the accuracy of the included equipment by calling us prior to purchase. <br />",
   "url":"https://www.het/new-Hamilton-2022-Honda-Ridgeline-RTL AWD-5FPYK3F59NB028624",
   "image":"https://www.otos/3287/5fpyk3f59nb028624/ip/1.jpg",
   "productID":"5FPYK3DAF59NB028624",
   "sku":"NBDDFX028624",
   "itemCondition":"New",
   "brand":"Honda",
   "model":"Ridgeline",
   "mpn":"YK3F5NJNW",
   "aggregateRating":{
      "@type":"AggregateRating",
      "ratingValue":"4.73",
      "reviewCount":"26"
   },
   "review":{
      "@type":"Review",
      "reviewRating":{
         "@type":"Rating",
         "ratingValue":"4.73",
         "bestRating":"5",
         "worstRating":"1"
      },
      "author":{
         "@type":"Organization",
         "name":"Edmunds"
      }
   },
   "offers":{
      "@type":"Offer",
      "price":"42810",
      "availability":"https://schema.org/InStock",
      "itemCondition":"New",
      "priceCurrency":"USD",
      "priceValidUntil":"2022-06-30",
      "url":"https://www.ton-2022-Honda-Ridgeline-RTL AWD-5FPYK3F59NB028624",
      "availableAtOrFrom":{
         "@type":"AutoDealer",
         "@id":"https://wnhonda.net",
         "name":"Hamilton Honda",
         "address":{
            "@type":"PostalAddress",
            "streetAddress":"6y 130",
            "addressLocality":"Haon",
            "addressRegion":"NJ",
            "postalCode":"091",
            "addressCountry":"US"
         },
         "geo":{
            "@type":"GeoCoordinates",
            "latitude":408691406,
            "longitude":-7453308105469
         },
         "url":"https:da.net",
         "telephone":" 27020",
         "brand":"Honda",
         "logo":"httogo.png",
         "image":"hta.net/static/dealer-11512/logo.png",
         "description":"Hamealer in Hamilton, NJ. We specialize in new Honda, used vehicles, service, and financing.",
         "sameAs":[
            "https://wwonHonda",
            "https://www.facebHondaNJ"
         ],
         "openingHoursSpecification":[
            {
               "@type":"OpeningHoursSpecification",
               "dayOfWeek":"Monday",
               "opens":"09:00",
               "closes":"21:00"
            },
            {
               "@type":"OpeningHoursSpecification",
               "dayOfWeek":"Tuesday",
               "opens":"09:00",
               "closes":"21:00"
            },
            {
               "@type":"OpeningHoursSpecification",
               "dayOfWeek":"Wednesday",
               "opens":"09:00",
               "closes":"21:00"
            },
            {
               "@type":"OpeningHoursSpecification",
               "dayOfWeek":"Thursday",
               "opens":"09:00",
               "closes":"21:00"
            },
            {
               "@type":"OpeningHoursSpecification",
               "dayOfWeek":"Friday",
               "opens":"09:00",
               "closes":"21:00"
            },
            {
               "@type":"OpeningHoursSpecification",
               "dayOfWeek":"Saturday",
               "opens":"09:00",
               "closes":"18:00"
            },
            {
               "@type":"OpeningHoursSpecification",
               "dayOfWeek":"Sunday"
            }
         ],
         "department":[
            {
               "name":"Sales",
               "@type":"AutoDealer",
               "image":"https:/ler-11512/logo.png",
               "telephone":"6093627020",
               "url":"https.net/",
               "address":{
                  "@type":"PostalAddress",
                  "streetAddress":"65 130",
                  "addressLocality":"Haton",
                  "addressRegion":"NJ",
                  "postalCode":"081",
                  "addressCountry":"US"
               },
               "openingHoursSpecification":[
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Monday",
                     "opens":"09:00",
                     "closes":"21:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Tuesday",
                     "opens":"09:00",
                     "closes":"21:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Wednesday",
                     "opens":"09:00",
                     "closes":"21:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Thursday",
                     "opens":"09:00",
                     "closes":"21:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Friday",
                     "opens":"09:00",
                     "closes":"21:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Saturday",
                     "opens":"09:00",
                     "closes":"18:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Sunday"
                  }
               ]
            },
            {
               "name":"Service",
               "@type":"AutoRepair",
               "image":"httaler-11512/logo.png",
               "telephone":"6093627060",
               "url":"ht/service.aspx",
               "address":{
                  "@type":"PostalAddress",
                  "streetAddress":"6 130",
                  "addressLocality":"Hamon",
                  "addressRegion":"NJ",
                  "postalCode":"01",
                  "addressCountry":"US"
               },
               "openingHoursSpecification":[
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Monday",
                     "opens":"07:00",
                     "closes":"19:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Tuesday",
                     "opens":"07:00",
                     "closes":"19:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Wednesday",
                     "opens":"07:00",
                     "closes":"19:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Thursday",
                     "opens":"07:00",
                     "closes":"19:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Friday",
                     "opens":"07:00",
                     "closes":"19:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Saturday",
                     "opens":"07:00",
                     "closes":"18:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Sunday"
                  }
               ]
            },
            {
               "name":"Parts",
               "@type":"AutoPartsStore",
               "image":"httt/static/dealer-11512/logo.png",
               "telephone":"609360",
               "url":"https://ws-department.html",
               "address":{
                  "@type":"PostalAddress",
                  "streetAddress":"655  130",
                  "addressLocality":"Han",
                  "addressRegion":"NJ",
                  "postalCode":"01",
                  "addressCountry":"US"
               },
               "openingHoursSpecification":[
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Monday",
                     "opens":"07:00",
                     "closes":"19:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Tuesday",
                     "opens":"07:00",
                     "closes":"19:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Wednesday",
                     "opens":"07:00",
                     "closes":"19:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Thursday",
                     "opens":"07:00",
                     "closes":"19:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Friday",
                     "opens":"07:00",
                     "closes":"19:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Saturday",
                     "opens":"07:00",
                     "closes":"18:00"
                  },
                  {
                     "@type":"OpeningHoursSpecification",
                     "dayOfWeek":"Sunday"
                  }
               ]
            }
         ]
      }
   },
   "color":"Pacific Pewter Metallic"
}

Anyone know how to solve this problem?

CodePudding user response:

Your problem is that you don't know how to both return a result and keep looking for the next one. And you also aren't using arrays for "many results".

Using generators makes the first problem straightforward to solve. And for the second, that's just a mindset you'll learn.

Here is fixed code.

def recurseitem(obj, value):
    if '@type' in obj:
        if obj['@type'] in value:
            yield obj
    for k, v in obj.items():
        if isinstance(v,dict):
            for item in recurseitem(v, value):
                yield item

def get_results(data): # data = []
    results = {}

    valuetype_to_find = {
        'product': ['product', 'Product', 'car', 'Car'],
        'AutoDealer': ['AutoDealer'],
        'PostalAddress': ['PostalAddress'],
        'offer': ['Offer', 'offer', 'Offer',]
    }

    for vk, vt in valuetype_to_find.items():
        results[vk] = list(recurseitem(data, vt))

    return results
  • Related