如何读取plist中的响应字符串?
How to read response strings in plist?
我正在尝试解析 plist 以获取字段的响应字符串 "What Change bugs are fixed in this submission? "
如下但不知何故它总是空的?有人可以提供有关错误原因的指导吗?
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()")))
试试这个:
plist = [your snippet above]
root = et.fromstring(plist)
resp = root.xpath('//key[text()="response"]/following-sibling::string')
for i in resp:
print(i.text)
输出:
<change://problem/45317899> hostapd to include IOKit framework
<change://problem/35143400> Yukon: hostapd-33 contains references to deprecated TARGET_OS_EMBEDDED macro
我正在尝试解析 plist 以获取字段的响应字符串 "What Change bugs are fixed in this submission? " 如下但不知何故它总是空的?有人可以提供有关错误原因的指导吗?
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()")))
试试这个:
plist = [your snippet above]
root = et.fromstring(plist)
resp = root.xpath('//key[text()="response"]/following-sibling::string')
for i in resp:
print(i.text)
输出:
<change://problem/45317899> hostapd to include IOKit framework
<change://problem/35143400> Yukon: hostapd-33 contains references to deprecated TARGET_OS_EMBEDDED macro