Python Forum
Accessing nested dictionary values. Plistlib, Python 2.7
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing nested dictionary values. Plistlib, Python 2.7
#30
I think I may have found some hackish way around this problem....


I found a "print_dict" function online on another forum last night.


def print_dict(v, prefix=''):
    if isinstance(v, dict):
        for k, v2 in v.items():
            p2 = "{}['{}']".format(prefix, k)
            print_dict(v2, p2)
    elif isinstance(v, list):
        for i, v2 in enumerate(v):
            p2 = "{}[{}]".format(prefix, i)
            print_dict(v2, p2)
    else:
        print('{} = {}'.format(prefix, repr(v)))
for y in candidates2:
    for item in y['_items']:
        for item2 in item['_items']:
            HDDetails = print_dict(item2)
            
the console outputs

['smart_status'] = 'Failing'
['detachable_drive'] = 'no'
['spsata_rotational_rate'] = 7200
['spsata_ncq_depth'] = '32'
['device_model'] = 'ST3500418ASQ                            '
['size_in_bytes'] = 500107862016
['_name'] = 'ST3500418ASQ'
... and so on
I can try saving the console output to a text file using sys.stdout and then later in the same script (outside the for loop) reopening the file and using string methods to chop it up and get the hard drive information I need.

I know the xml/plist data is no fun to read but I'm certain there is an easier way of getting access to this info?
Reply


Messages In This Thread
RE: Accessing nested dictionary values. Plistlib, Python 2.7 - by williamlombard - Sep-29-2017, 05:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Accessing method attributes of python class Abedin 6 1,264 Apr-14-2025, 07:02 AM
Last Post: buran
  Replace values in Yaml file with value in dictionary PelleH 1 2,342 Feb-11-2025, 09:51 AM
Last Post: alexjordan
Question Using Lists as Dictionary Values bfallert 8 2,464 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  need to compare 2 values in a nested dictionary jss 2 1,875 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 2,982 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  Accessing same python script from multiple computers bigrockcrasher 1 3,074 May-25-2022, 08:35 PM
Last Post: Gribouillis
  Python modules for accessing the configuration of relevant paths Imago 1 2,539 May-07-2022, 07:28 PM
Last Post: Larz60+
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 2,817 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  plistlib / xml file / get value except key Tecuma 6 4,732 May-26-2021, 03:51 PM
Last Post: Tecuma
  Nested dictionary acting strange Pedroski55 2 3,034 May-13-2021, 10:37 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020