I am parsing this XML data, created by/for another application:
<Data>
<Description>Hello\nWorld<Description>
</Data>
If I find the string, it converts the \n
to \\n
, so \n appears in the output instead of the intended newline:
import xml.etree.ElementTree as ET
...
root = ET.parse(xmldata).getroot()
print(root.find('Description').text)
Output is:
Hello\nWorld
My immediate workaround is to unescape the backslash:
print(root.find('Description').text.replace('\\n','\n'))
Output is:
Hello
World
But is there a more correct way? I have the sense there is a more correct way to do this.