python - 查找 plist 中是否包含具有特定字符串的键

标签 python plist

下面我有一个 xml/plist 文件(您可能会将其识别为 .tmTheme):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>settings</key>
<array>
    <dict>
        <key>name</key>
        <string>Comment</string>
        <key>scope</key>
        <string>comment</string>
        <key>settings</key>
        <dict>
            <key>foreground</key>
            <string>#75715E</string>
        </dict>
    </dict>
    <dict>
        <key>name</key>
        <string>String</string>
        <key>scope</key>
        <string>string</string>
        <key>settings</key>
        <dict>
            <key>foreground</key>
            <string>#E6DB74</string>
        </dict>
    </dict>
</array>
</dict>
</plist>

我试图查找每个字典是否包含字符串scope,以及scope是否具有特定字符串,例如comment。然后我需要进入内部字典并检索 key foreground。在这种情况下,我需要检索 #75715E

我首先使用a = pliSTLib.readPlist(),然后使用b = a["settings"]。然后我不确定应该如何处理它,请记住我稍后需要找到根字典,以便我可以在其中获取其他字典。

最佳答案

my_plist = plistlib.readPlist()
settings = my_plist["settings"]
for d in settings:
    if "scope" in d:
        if "comment" not in d["scope"]:
            print "A scope with no comment!"
    else:
        print "A dict with no scope!"

关于python - 查找 plist 中是否包含具有特定字符串的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11371415/

相关文章:

Python段错误,使用pyqt4

ios 保存在 plist 中 vs 保存在 nsdata 中

iphone - 从 Plist 中读取信息以在 TableView 中使用

xcode - 将plist读入nsmutabledictionary,更新字典然后再次保存

插入数据库时​​Python嵌套循环错误

python - datetime.datetime.ctime() 函数返回固定宽度的字符串吗?

swift - Xcode/plist - 在 macOS 上始终以深色/浅色主题运行应用程序

ios - 如何仅在 Debug模式下启用3D触摸(静态快速操作)?

python - 如何绘制日期列表的直方图?

python - 将 numpy 数组中的元素四舍五入为另一个数组中相同索引的个数