user715578
user715578

Reputation: 467

Save python plistlib data (revised)

How can I use this working code:

import plistlib, time
import zipfile


from contextlib import closing
import os



with closing(zipfile.ZipFile("fw.zip")) as zfile:
     for info in zfile.infolist():
        if info.filename.endswith('Restore.plist'):
            zfile.extract(info)

import plistlib as pl
p=pl.readPlist("Restore.plist")
print p["ProductType"]
print p["ProductVersion"]
print p["ProductBuildVersion"]
outputfile = open('output.txt', 'w')
outputfile.write( p["ProductVersion"] )
outputfile.write( ' ')
outputfile.write( p["ProductType"] )
outputfile.write( ' ')
outputfile.write( p["ProductBuildVersion"] )
outputfile.close()

and use it to write out the string under the "update" key in this plist, seeing how update key is under another set of keys called "RestoreRamDisks"?

<key>RestoreRamDisks</key>#this is the key that the "update" key is under
<dict>
    <key>Update</key>#here is the update key I'm talking about
    <string>018-7074-092.dmg</string>#this is what I want python to spit out
    <key>User</key>
    <string>018-7082-092.dmg</string>
</dict>

To clarify, I just want use the same method above, this time, to get the info of the "update" key. The part that confuses me is that the "update" key is underneath another key called "RestoreRamDisks". I want this program to spit out 018-7074-092.dmg when asked to locate the "update" key..

Upvotes: 0

Views: 376

Answers (1)

Sean Vieira
Sean Vieira

Reputation: 160043

What happens when you print p["RestoreRamDisks"]? You can probably get the value by simply doing

p["RestoreRamDisks"]["Update"]
# If your plist may not contain a key called "RestoreRamDisks"
# use this instead
p.get("RestoreRamDisks", dict(Update=None)).get("Update", None)
# This will get the value if available, otherwise it will
# evaluate to None

Upvotes: 1

Related Questions