0

I have a string that when parsed with tostring gives me a bunch of xml. Is there a way to get that fixed?

For example, when i run ET.tostring(elementObject,,encoding='us-ascii', method='xml'), i get <string>11&gt;2</string>. I am expecting: "11>2". Is there a way to get what I am expecting or do I need to do some regex on that return value?

I am using import xml.etree.ElementTree as ET

thanks

1 Answer 1

2

ET.tostring() is meant to produce a XML string for a given element, not the contained string value. Use the Element.text attribute instead:

elementObject.text

Demo:

>>> from xml.etree import ElementTree as ET
>>> root = ET.fromstring('''\
... <root><string>11&gt;2</string></root>''')
>>> root.find('string')
<Element 'string' at 0x103332990>
>>> root.find('string').text
'11>2'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.