我正在尝试编写一个Python脚本,用于在XML输出中打印特定标记的值。这里,我需要打印的标记值是XML输出中每次出现的值。我尝试如下,但它显示属性错误。这可能有什么问题?获取和打印某些我感兴趣的标签值的正确方法是哪种?有什么帮助吗?谢谢。
import xml.etree.ElementTree as ET
mystring="""<?xml version="1.0" encoding="UTF-8"?>
<main>
<student>
<male>
<result>pass</result>
<name>Paul</name>
<address>boston</address>
<localreference>
<name>Charlie</name>
</localreference>
</male>
<female>
<result>pass</result>
<name>Rose</name>
<address>newyork</address>
<localreference>
<name>Charlie</name>
</localreference>
</female>
</student>
<student>
<male>
<result>fail</result>
<name>Philippe</name>
<address>boston</address>
<localreference>
<name>White</name>
</localreference>
</male>
</student>
</main>"""
main = ET.fromstring(mystring)
for student in main:
if (student.tag == "student"):
print student.find("male/result").text
print student.find("female/result").text
错误&GT;
# python new5.py
pass
pass
fail
Traceback (most recent call last):
File "new5.py", line 39, in <module>
print student.find("female/result").text
AttributeError: 'NoneType' object has no attribute 'text'
答案 0 :(得分:0)
您打印标记值的代码是正确的,但是您要求xml的一部分不存在。第二个学生部分没有女性部分。这就是student.find("female/result")
向第二位学生返回None
的原因,而您无法在.text
个对象上调用None
。
答案 1 :(得分:0)
ElementTree支持XPath的子集,对您的示例可能更容易:
root = ET.fromstring(mystring)
for gender in ('male', 'female'):
print gender
for student in root.findall('./student/%s' % gender):
print '\t{:20}: {}'.format(student.find('name').text, student.find('result').text)
打印:
male
Paul : pass
Philippe : fail
female
Rose : pass
(顺便说一下:避免使用main
作为变量名,因为你破坏了main
模块的名称)
如果您希望按文档顺序排列结果而不是按性别分组,则可能会执行以下操作:
for students in root.findall('./student'):
for gender in students:
print ' '.join([gender.tag] + map(lambda a: gender.find(a).text, ('name', 'address', 'result', 'localreference/name')))
打印
male Paul boston pass Charlie
female Rose newyork pass Charlie
male Philippe boston fail White