[Python] Not able to retain ' in the xml ElementTree write in python

Discussão em 'Python' iniciado por Stack, Fevereiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    After parsing an xml string with an attribute value containing the ' characters, when I try to write it to a file or output it as a string, I am not able to retain ' occurrences. I know, the final xml is still valid, but from the diff perspective, I want to retain the ' values. Any tips?

    Here is a sample code,

    import xml.etree.ElementTree as ETree

    xml_str = '''<?xml version="1.0" encoding="UTF-8"?>
    <testItem name="SomeName" description="Change to &apos;&apos;Calculated&apos;&apos; in the Diagram tab">
    </testItem>
    '''

    root = ETree.fromstring(xml_str)
    tree = ETree.ElementTree(root)
    ETree.tostring(root, encoding='utf-8')
    # tree.write('output.xml') # this also doesn't work, writes single quotes to the file


    I get the below output,

    b'<testItem name="SomeName" description="Change to \'\'Calculated\'\' in the Diagram tab">\n</testItem>'

    Continue reading...

Compartilhe esta Página