读取.plist文件数据并使用python记录到内存

时间:2011-09-13 07:40:19

标签: python cmd

我有一个zip文件。在那个zip中,我有一个plist文件,“Restore.plist”。我怎么能告诉python阅读 该.plist文件并在到达此部分时停止:

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

我想告诉Python停在“SupportedProductTypes”键并将其对应的字符串“iPhone3,1”作为变量记录到内存中,例如“x” 然后打印x。我怎么能这样做?

2 个答案:

答案 0 :(得分:3)

Python标准库中的

plistlib处理.plist个文件。有关快速教程,请参阅this PyMOTW article

另请参阅ZipFile模块中的zipfile类(也在Python标准库中),以便从ZIP文件中读取。

答案 1 :(得分:1)

尝试使用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

给出:

iPhone3,1

使用名为“keys.xml”的测试文件:

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