python - 如何读取plist中的响应字符串?

标签 python xml lxml plist elementtree

我正在尝试解析 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>&lt;change://problem/45317899&gt; hostapd to include IOKit framework
&lt;change://problem/35143400&gt; 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)

预期输出:-

&lt;change://problem/45317899&gt; hostapd to include IOKit framework
&lt;change://problem/35143400&gt; 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()"))) 

关于python - 如何读取plist中的响应字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57318382/

相关文章:

c# - 从XML到具有不规则xml的C#的信息

python lxml元素属性问题

python - Django 中的 "Master page"管理

python - 使用python中的正则表达式从字符串中提取不同格式的日期

android - 在 xml 中使用比率键线

android - AppCompat 工具栏不显示菜单 XML 中的图标集

Python lxml : How to split comma separated data and find specific values from XML-file?

python - 为什么我的 python 请求检索到不正确的 xml?

python - 因 pdb 中的异常而暂停

python - 我可以使用 mod_python 在 Apache 服务器上运行 Flask 吗?