As the documentation says, if you want to read only the text, without any intermediate tags, you have to recursively concatenate all text
and tail
attributes in the correct order.
However, recent-enough versions (including the ones in the stdlib in 2.7 and 3.2, but not 2.6 or 3.1, and the current released versions of both ElementTree
and lxml
on PyPI) can do this for you automatically in the tostring
method:
>>> s = '''<tag>
... Some <a>example</a> text
... </tag>'''
>>> t = ElementTree.fromstring(s)
>>> ElementTree.tostring(s, method='text')
'\n Some example text\n'
If you also want to strip whitespace from the text, you'll need to do so manually. In your simple case, that's easy:
>>> ElementTree.tostring(s, method='text').strip()
'Some example text'
In more complicated cases, however, where you want to strip out whitespace within intermediate tags, you'll probably have to fall back on recursively processing the text
s and tail
s. That's not too hard; you just have to remember to deal with the possibility that the attributes may be None
. For example, here's a skeleton you can hook your own code on:
def textify(t):
s = []
if t.text:
s.append(t.text)
for child in t.getchildren():
s.extend(textify(child))
if t.tail:
s.append(t.tail)
return ''.join(s)
This version only works when text
and tail
are guaranteed to be a str
or None
. For trees you build up manually, that's not guaranteed to be true.
re.sub(r'\<.*?\>', '', text)
. – Miso