我有一个zip文件。在那个zip中,我有一个plist文件,“Restore.plist”。我怎么能告诉python阅读 该.plist文件并在到达此部分时停止:
<key>SupportedProductTypes</key>
<array>
<string>iPhone3,1</string>
</array>
我想告诉Python停在“SupportedProductTypes”键并将其对应的字符串“iPhone3,1”作为变量记录到内存中,例如“x” 然后打印x。我怎么能这样做?
答案 0 :(得分:3)
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>