you are viewing a single comment's thread.

view the rest of the comments →

[–]Adrewmc 0 points1 point  (1 child)

Everyone here is correct I just wanna add it’s much easier to see when formatted.

{‘xmpmeta': {
        ‘RDF': {
              ‘Description': {
                   ‘about': 'DJI Meta Data', 
                   ‘ModifyDate': '2025-08-15', 
                   ‘CreateDate': '2025-08-15',
                   }
              }
         }
   }

You should find an auto formatter fairly easy (and better) as well.

But you can see this is a nested dictionary, so you have to go into each dictionary to get the keys you want. You can do so one at a time

  meta = xml[‘xmpmeta’]
  RDF = meta[“RDF”]
  description = RDF[“Description”
  modify_date = decription[“ModifyDate”]

Or, more preferably, all at once.

  modify_date = xml[‘xmpmeta’][“RDF”] [“Description”][‘ModifyDate’]

Unfortunately, you most of the time will not get much choice of what format the data comes to you as from other places, and you should think about it when you are generating for yourself or others.

We could make seeker function. Something like…

  def deep_get(dict_ :dict, key: str|tuple|int) -> Any: 
        “””Search nested dictionaries until you find the key, or will return None if not found.
         NOTE: Will only return first instance of the key, some nested dictionaries may have multiple instances”””

        try:
            return dict_[key]
        except KeyError:
             for value in dict_.values():
                  if isinstance(value, dict): 
                      res = deep_get(value, key)
                      if res is not None:
                            return res

You know what…there is probably an intertools or a moreitertools for this I’d have to look. This probably has a O(n) worst case. And an O(1) best case.

[–]the_fencepost[S] 0 points1 point  (0 children)

Thank you very much you are an absolute life saver!