I am trying to write a Python script that prints the value of a particular tag in an XML output. Here, the tag value I need to print is the value of in each and every occurrences in the XML output. I tried as below, but it shows an attribute error. What could be wrong here? Which is the correct way of getting and print values of certain more tags which I am interested to see? Any help please? Thanks.
我正在尝试编写一个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
Error>
# 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'
2 个解决方案
#1
0
ElementTree supports a subset of XPath, and that may be easier for your example:
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)
Prints:
male
Paul : pass
Philippe : fail
female
Rose : pass
(btw: avoid using main
as a variable name since you clobber the name of the main
module)
(顺便说一句:避免使用main作为变量名,因为你破坏了主模块的名称)
If you want the results in document order rather than grouped by gender, you might do something like:
如果您希望按文档顺序排列结果而不是按性别分组,则可能会执行以下操作:
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')))
Prints
male Paul boston pass Charlie
female Rose newyork pass Charlie
male Philippe boston fail White
#2
0
Your code for printing a tag value is correct, but you are asking for a section of your xml that does not exist. There is no female section in the second student section. This is why student.find("female/result")
is returning None
on the second student, and you cannot call .text
on a None
object.
您打印标记值的代码是正确的,但是您要求xml的一部分不存在。第二个学生部分没有女性部分。这就是为什么student.find(“female / result”)在第二个学生上返回None,并且你不能在None对象上调用.text。
#1
0
ElementTree supports a subset of XPath, and that may be easier for your example:
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)
Prints:
male
Paul : pass
Philippe : fail
female
Rose : pass
(btw: avoid using main
as a variable name since you clobber the name of the main
module)
(顺便说一句:避免使用main作为变量名,因为你破坏了主模块的名称)
If you want the results in document order rather than grouped by gender, you might do something like:
如果您希望按文档顺序排列结果而不是按性别分组,则可能会执行以下操作:
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')))
Prints
male Paul boston pass Charlie
female Rose newyork pass Charlie
male Philippe boston fail White
#2
0
Your code for printing a tag value is correct, but you are asking for a section of your xml that does not exist. There is no female section in the second student section. This is why student.find("female/result")
is returning None
on the second student, and you cannot call .text
on a None
object.
您打印标记值的代码是正确的,但是您要求xml的一部分不存在。第二个学生部分没有女性部分。这就是为什么student.find(“female / result”)在第二个学生上返回None,并且你不能在None对象上调用.text。