下面我有一个 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/