1

下面我有一个 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()then b = a["settings"]。然后我不知道我应该如何处理它,记住我需要稍后找到根字典,以便我可以在其中获取另一个字典。

4

1 回答 1

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!"
于 2012-07-07T00:58:40.813 回答