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

2024-09-30 18:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个压缩文件。在那个压缩包里,我有一个plist文件,“还原.plist". 我如何告诉python阅读 当.plist文件到达此部分时停止:

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

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


Tags: 文件key内存字符串string记录arrayplist
2条回答

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

给出:

^{pr2}$

此测试文件名为“密钥.xml“:

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

Python标准库中的plistlib处理.plist文件。有关快速教程,请参阅this PyMOTW article。在

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

相关问题 更多 >