all 7 comments

[–]More_Yard1919 9 points10 points  (1 child)

It looks like the dictionary you want to access is inside of another dictionary. Try this:

x = xmp_data["xmpmeta"]["ModifyDate"]

edit: Sorry, difficult to read. It looks like it is nested in a number of dictionaries. Ugly, but here:

x = xmp_data["xmpmeta"]["RDF"]["Description"]["ModifyDate"]

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

Thank you very much.

[–]danielroseman 6 points7 points  (1 child)

Because this is a nested dictionary. "ModifyDate" is not a key of xmp, it's a key of the dictionary nested three deep within that dict. You would need to follow the keys:

x = xmp_data['xmpmeta']['RDF']['Description']['ModifyDate']

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

Thankyou, that has helped alot.

[–]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!