我有一个函数,它接受一个名为label的参数。然后使用ElementTree来替换元素“assignedNode”的内容。在
该函数循环遍历目录中的所有xml文件,并将“label”参数写入元素内容。在
我的“label”参数包含和符号,但当ET将其写入xml文件时,它显示为“amp;amp”。在
有人能告诉我如何取消这个字符的转义并强制它写“&;”符号吗?在
这是我的代码:
def apply_label(label):
clone_path = os.getcwd()
for root, dirs, files in os.walk(clone_path):
for f in files:
try:
tree = ET.ElementTree(file=f)
root = tree.getroot()
for assignedNode in root.iter("assignedNode"):
assignedNode.text = label
tree = ET.ElementTree(root)
with open(f, "w") as a:
tree.write(a)
except :
pass
以下是XML格式的转义字符列表:
https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
&
是转义字符,默认情况下,ElementTree解析器设计为转义此字符参考号:What characters do I need to escape in XML documents?
相关问题 更多 >
编程相关推荐