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

时间:2012-07-07 00:54:29

标签: 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。然后我需要转到内部字典并检索密钥foreground。在这种情况下,我需要检索#75715E

我开始使用a = plistlib.readPlist()然后使用b = a["settings"]。然后我不确定我应该如何处理它,记住我需要稍后找到根词典,这样我就可以在其中获得其他字典。

1 个答案:

答案 0 :(得分:1)

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!"