简体   繁体   中英

Read .plist file data and record to memory with python

I have a zip file. In that zip, I have a plist file, "Restore.plist". How can I tell python to read that .plist file and stop when it reaches this part:

<key>SupportedProductTypes</key>
<array>
    <string>iPhone3,1</string>
</array>

I Want to tell Python to stop at the "SupportedProductTypes" key and record its corresponding string, "iPhone3,1" to memory as a variable, such as "x" then print x. How can I do this?

plistlib in the Python standard library deals with .plist files. Refer to this PyMOTW article for a quick tutorial.

See also the ZipFile class in the zipfile module (also in the Python standard library) for reading from ZIP files.

Try with xml :

import xml.etree.ElementTree as ET

tree = ET.parse("keys.xml")
doc = tree.getroot()
sup = doc.getiterator("SupportedProductTypes")

print doc.getiterator("string")[0].text

gives:

iPhone3,1

with this test file called "keys.xml" :

<keys>
<key>SupportedProductTypes</key>
<array>
    <string>iPhone3,1</string>
</array>
</keys>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM