Q:

Replacing XML entity (& < >  ) with a placeholder

I have a XML file like this;
<Table>
<Row>
    <Cell>text id</Cell>
    <Cell>First Text&lt;br/&gt;&amp;nbsp;&lt;br/&gt;</Cell>
    <Cell>Second Text&lt;br/&gt;&amp;nbsp;&lt;br/&gt;</Cell>
</Row>
.
.
</Table>

With using these code, I am sending every text inside of <Cell></Cell> to a list variable.
tree = ET.parse(file_path)
root = tree.getroot()
list = []
for row in root.iter(tag='Row'):
    for cell in row:
        list.append(cell.text)

Problem is First Text&lt;br/&gt;&amp;nbsp;&lt;br/&gt; becomes to First Text<br/>&nbsp;<br/>
I want to replace these entity characters with placeholders assigned by myself because I have to replace them to original form later in my program. For Example;
special character -> [placeholder]
 &lt;br/&gt;       -> [br/]
 &amp;             -> [amp]
 &nbsp;             -> [nbsp]

A:

I found way to do it. Before XML parsing, I read every line with readlines() then replace entity characters with placeholders after that write line in a new xml file. After translation I can replace them with original forms with using replace().
First Text&lt;br/&gt;&amp;nbsp;&lt;br/&gt; => First Text[H1][H4][H6][H1]
with open(r"C:\Users\USER NAME\PycharmProjects\FILE TEST\edited_file.xml", "w", encoding="utf-8") as file2:
    pass

with open(r"C:\Users\USER NAME\PycharmProjects\FILE TEST\source_file.xml", "r", encoding="utf-8") as file1:
    lines = file1.readlines()
    text = ""
    for i in lines:
        text = i
        text = text.replace('&lt;br/&gt;', '[H1]')
        text = text.replace('&lt;', '[H2]')
        text = text.replace("&gt;", "[H3]")
        text = text.replace("&amp;", "[H4]")
        text = text.replace("&nbsp;", "[H5]")
        text = text.replace("nbsp;", "[H6]")
        with open(r"C:\Users\USER NAME\PycharmProjects\FILE TEST\edited_file.xml", "a", encoding="utf-8") as file2:
            file2.writelines(text)

