【发布时间】:2017-10-15 21:26:00
【问题描述】:
我有一个 XML 文件,我需要在 Python 中提取特征标签内的所有文本
<person>
<text id="1">
<title>
student
</title>
<feature>
xxxx
<name>yyyy</name>
zzzz
<country>dddd</country>
ffff
</feature>
我的代码是这样的:
for person in tree.iter():
for text in person:
for feature in text:
if feature.tag=="feature":
print(feature.text)
它只是显示“xxxx”,但我的理想答案是 xxxx yyyy zzzz dddd ffff
【问题讨论】:
-
让一个 XML 节点既包含文本内容又包含其他类似的 XML 节点是非常不寻常的。可能是您遇到问题的原因。
标签: xml python-3.x xml-parsing