python ElementTree有一个孩子的元素的文本 [英] python ElementTree the text of element who has a child
本文介绍了python ElementTree有一个孩子的元素的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试读取有孩子的元素的文本时,它没有显示内容:
When I try to read a text of a element who has a child, it gives None:
请参阅xml(例如test.xml):
See the xml (say test.xml):
<?xml version="1.0"?>
<data>
<test><ref>MemoryRegion</ref> abcd</test>
</data>
和想要读取'abcd'的python代码:
and the python code that wants to read 'abcd':
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
print root.find("test").text
当我运行此python时,
When I run this python, it gives None, rather than abcd.
在这种情况下如何读取abcd?
How can I read abcd under this condition?
推荐答案
使用 Element.tail
属性:
>>> import xml.etree.ElementTree as ET
>>> tree = ET.parse('test.xml')
>>> root = tree.getroot()
>>> print root.find(".//ref").tail
abcd
这篇关于python ElementTree有一个孩子的元素的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文