Given an Element from etree
, I would like to print only the opening tag. This would be immensely useful for debugging.
For example:
>>> from lxml import etree
>>> elem = etree.fromstring(b'''<bbq taste="awful" smell="amazing">
... <food type="hot dogs" />
... <food type="hamburgers" condition="burnt" />
... </bbq>''')
If I want to print this element I can use etree.tostring
, but it can only print the full tree, even if it’d take up 10 screens:
>>> print(etree.tostring(elem).decode())
<bbq taste="awful" smell="amazing">
<food type="hot dogs"/>
<food type="hamburgers" condition="burnt"/>
</bbq>
>>>
What I would like is something like this:
>>> print(etree.tostring(elem, open_tag_only=True).decode())
<bbq taste="awful" smell="amazing">
>>>