XML line break character entity and Python encoding

孤街浪徒 提交于 2019-12-13 23:36:46

问题


I have the following line of code in a python script:

dep11 = ET.SubElement(dep1, "POVCode").text = "#declare lg_quality = LDXQual;
#if (lg_quality = 3)
#declare lg_quality = 4;
#end"

My question is in regards to the 
 character. I want to see this character entity in the XML output, but the first ampersand keeps getting replaced with the & character entity, which creates the nonsense character entity 
.

I am encoding the file as utf-8.

import xml.etree.ElementTree as ET

...

with open("LGEO.xml", "wb") as f:
    tree.write(f, "utf-8")

And I end up with:

<POVCode>#declare lg_quality = LDXQual;&amp;#x0A;#if (lg_quality = 3)&amp;#x0A;#declare lg_quality = 4;&amp;#x0A;#end</POVCode>

Not sure what I'm doing wrong.

[edit]

I am trying to implement the solution found here that @mzjn pointed out.

How to output XML entity references

I have six lines of code:

dep11 = ET.SubElement(dep1, "POVCode")
dep21 = ET.SubElement(dep2, "POVCode")
dep11.text = "#declare lg_quality = LDXQual;&&#x0A;#if (lg_quality = 3)&&#x0A;#declare lg_quality = 4;&&#x0A;#end"
dep21.text = "#declare lg_studs = LDXStuds;&&#x0A;"
ET.tostring(dep11).replace('&amp;&amp;', '&')
ET.tostring(dep21).replace('&amp;&amp;', '&')

I get no error, but the result is not any different than before when I write the tree.

Again I am stuck.


回答1:


What I eventually did was use the standard Python write function instead of using ElementTree's own write function.

text = ET.tostring(root).replace("&amp;&amp;", "&")

with open("LGEO.xml", "wb") as f:
    f.write(text.encode("utf-8"))

The above is the last step in the Python code. I don't know if there are disadvantages to converting the root object to a string like this. It could be slower but it works for me.



来源:https://stackoverflow.com/questions/56892938/xml-line-break-character-entity-and-python-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!