How to extract text in nested xml after closing of one tag in python using xml.etree.ElementTree

时光毁灭记忆、已成空白 提交于 2019-12-11 23:07:03

问题


I want to extract all text in xml document, and I am having a problem for the following case:

...
<a>
hello
<B>
there
</B>
How was your day.

.....
</a>

In this snippet, I can get the text "hello" and "there" because I can get them using the following tags:

a.text
b.text

but I don't know how to access the "How was your day." part.


回答1:


You are looking for the .tail attribute of an element:

>>> from xml.etree import ElementTree
>>> example = ElementTree.fromstring('''\
... <a>
... hello
... <B>
... there
... </B>
... How was your day.
... </a>
... '''
... )
>>> example
<Element 'a' at 0x10715d150>
>>> example.text
'\nhello\n'
>>> example.find('B')
<Element 'B' at 0x10715d7d0>
>>> example.find('B').text
'\nthere\n'
>>> example.find('B').tail
'\nHow was your day.\n'


来源:https://stackoverflow.com/questions/16701546/how-to-extract-text-in-nested-xml-after-closing-of-one-tag-in-python-using-xml-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!