python - 如何读取 plist 中的响应字符串?
问题描述
我正在尝试解析 plist 以获取“此提交中修复了哪些更改错误?”字段的响应字符串,如下所示,但不知何故它总是为空?有人可以就错误的原因提供指导吗?
plist 片段:
<dict>
<key>description</key>
<string>What Change bugs are fixed in this submission? </string>
<key>id</key>
<string>7</string>
<key>multiline</key>
<string>1</string>
<key>releases</key>
<array>
<string>Yukon</string>
</array>
<key>response</key>
<string><change://problem/45317899> hostapd to include IOKit framework
<change://problem/35143400> Yukon: hostapd-33 contains references to deprecated TARGET_OS_EMBEDDED macro</string>
</dict>
代码:-
from lxml import etree as et
plistfile = '/Users/username/autosubmissionlogs/Yukon/02192019_200740/hostapd-34/hostapd-34.plist'
with open(plistfile) as raw:
# Parse the XML input file into a tree.
tree = et.parse(raw)
stringUsedAsKey = tree.xpath("/plist/dict/dict/string"
+ "[./text()=\"What Change bugs are fixed in this submission?\"]")[0]
interestingDict1 = stringUsedAsKey.getparent()
string = interestingDict1.xpath("key[text()=\"response\"]/following-sibling::string")[0]
print('Changes \n:'%string)
预期输出:-
<change://problem/45317899> hostapd to include IOKit framework
<change://problem/35143400> Yukon: hostapd-33 contains references to deprecated TARGET_OS_EMBEDDED macro
解决方案
问题是您打印结果的方式。这是正确的方法。
from lxml import etree as et
plistfile = '/Users/username/autosubmissionlogs/Yukon/02192019_200740/hostapd-34/hostapd-34.plist'
with open(plistfile) as raw:
# Parse the XML input file into a tree.
tree = et.parse(raw)
stringUsedAsKey = tree.xpath("/plist/dict/dict/string"
+ "[./text()=\"What Change bugs are fixed in this submission?\"]")[0]
interestingDict1 = stringUsedAsKey.getparent()
string = interestingDict1.xpath("key[text()=\"response\"]/following-sibling::string")[0]
print("{}{}".format('Changes \n:', string.xpath("text()")))
推荐阅读
- c# - 如何找到networkcomms DLL,我在Github 中找不到
- node.js - 字符串函数在 Multer 中不起作用
- ios - 无法将包含 CET 时区的字符串转换为 NSDate
- email - 通缉但未调用...实际上,与此模拟的交互为零
- java - 参数中带有类的Java调用函数
- c# - 为什么不可见的虚拟 ListView 的项目没有索引?
- flutter - Navigator 或 SlideTransition 用于从下到上显示屏幕
- swift - 如何制作多页 Swift Playground
- python - 如何在 kv 屏幕中锁定 Y 而不是 X?
- java - 使用spring boot获取外部api给null