【发布时间】:2017-12-04 08:05:27
【问题描述】:
尝试使用 xml.etree.ElementTree 附加现有 xml 文件时。子元素被写入两次。有什么办法可以克服。
这是我当前的代码:
with open('filename.aiml',"a+") as f:
tree=ET.parse(f)
root=tree.getroot()
rootTag=root.find('.')
category=ET.SubElement(rootTag,'category')
pattern=ET.SubElement(category,'pattern')
pattern.text=input_text.upper()
template=ET.SubElement(category,'template')
template.text=response
root.append(category)
tree.write(open("filename.aiml","w+"),encoding='ISO-8859-1')
写之前的XML:
<?xml version='1.0' encoding='ISO-8859-1'?>
<aiml version="1.0">
<category>
<pattern>WHAT IS DEEP LEARNING</pattern>
<template>Deep learning (also known as deep structured learning or hierarchical learning) is part of a broader family of machine learning methods based on learning data representations, as opposed to task-specific algorithms.</template>
</category>
写入后的XML:
<?xml version='1.0' encoding='ISO-8859-1'?>
<aiml version="1.0">
<category>
<pattern>WHAT IS DEEP LEARNING</pattern>
<template>Deep learning (also known as deep structured learning or hierarchical learning) is part of a broader family of machine learning methods based on learning data representations, as opposed to task-specific algorithms.</template>
</category>
<category>
<pattern>WHAT IS PROOF OF CONCEPT</pattern>
<template>Proof of Concept (PoC) is a realization of a certain method or idea in order to demonstrate its feasibility.</template>
</category>
<category><pattern>TELL ME ABOUT POC</pattern>
<template>Proof of Concept (PoC) is a realization of a certain method or idea in order to demonstrate its feasibility</template>
</category>
<category><pattern>WHAT IS PROOF OF CONCEPT</pattern>
<template>Proof of Concept (PoC) is a realization of a certain method or idea in order to demonstrate its feasibility</template>
</category>
<category><pattern>TELL ME ABOUT POC</pattern>
<template>Proof of Concept (PoC) is a realization of a certain method or idea in order to demonstrate its feasibility.</template></category>
【问题讨论】:
-
永远不要使用
open()读取 XML 文件。使用tree = ET.parse('path/to/file')。后者正确处理文件编码,前者没有。后者也更短。话虽如此,您不能“附加”到 XML 文件。加载树、修改树、保存树。 -
@Tomalak 我需要将新元素添加到现有的 XML 文件中。但是当我不使用
tree.write(open("filename.aiml","w+"),encoding='ISO-8859-1')时,它会显示 XML 解析错误 -
你在看我说的吗?我说你不能追加到 XML 文件。
-
其实上面的代码可以用来追加到现有的xml文件。唯一的问题是子元素被写入了两次。
-
不,它不能。但显然你知道自己在做什么,所以继续吧。
标签: python xml python-2.7 aiml