简体   繁体   English

如何使用xml.etree.ElementTree关闭python中的一个标签后如何提取嵌套xml中的文本

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

I want to extract all text in xml document, and I am having a problem for the following case: 我想提取xml文档中的所有文本,并且在以下情况下出现问题:

...
<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: 在此代码段中,我可以获取文本“ hello”和“ there”,因为可以使用以下标记获取它们:

a.text
b.text

but I don't know how to access the "How was your day." 但我不知道如何访问“您今天过的怎么样”。 part. 部分。

You are looking for the .tail attribute of an element: 您正在寻找元素的.tail属性

>>> 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM