Using plistlib to access 1 key with out the other nested keys after it

385 views Asked by At

This is going to be a stupid one.

I want to get "YES" out of the Plist below:

<key>Sample</key>
    <dict>
        <key>No</key>
        <dict>
            <key>Still No</key>
            <dict>
                <key>YES</key>
                <array>
                    <dict>
                        <key>Dont care</key>
                    </dict>
            </dict>
        </dict>
    </dict>

using plist lib i can do:

import plistlib as pl
pplist = pl.readPlist(plistPath)
pplist['Sample']['No']['Still No']
{'YES': {'DontCare'}}

I will get "YES" and "Dont care" but since its not just a standard list i cant access it using [0]

using a for loop though i can get just "YES" but i feel like there should be a lot cleaner way to do this then that.

for x in pplist['Sample']['No']['Still No']:
    thisIsYES = x
    break

print x
'YES'
1

There are 1 answers

1
Ture Pålsson On BEST ANSWER

It's a bit unclear what you want to happen if the data structure is not exactly as in your example (what if there is more than one key in the dict?), but my guess is that you want to look at the .keys() method of the dict (or dict-like object, I'm not sure) you get out of plistlib.