pythonxmlreplaceescapingxml-entities

How to include "&" in XML file


I need the following output in an XML

log="+ Passed Open the Website
"

Here is the relevant part of the code

var1 = '
'
var2 = f' + {res} {na}{var1}'
var2 = var2.replace('&', '&')
case = ET.SubElement(
    suite,
    "testcase",
    {
        "name": f"{na}",
        "log": f'{var2}'

I have tried .replace and escape but every time it shows this output.

log=" + Passed Load Home Page
"

How can I replace the &amp with &?


Solution

  • If your output is 
 then it means that you are re-escaping.
    Try to unescape before outputing it, e.g.:

    var1 = unescape('
')