我有以下 xml 代码:
<data factor="1" name="ini" value="342" />
我想复制相同的信息,但名称不同。即,最终输出应该是:
<data factor="1" name="ini" value="342" />
<data factor="1" name="raw_ini" value="342" />
我尝试执行以下操作:
model_tag = tree.findall(data_path) #I make sure that data_path is correct.
len_tags = len(model_tag)
i = 0
while i < len_tags:
tipo_tag = model_tag[i]
if tipo_tag.attrib['name']=='ini':
aux_tag = copy.deepcopy(tipo_tag) #I tried also with copy.copy(tipo_tag).
aux_tag.attrib['name'] = 'raw_ini'
model_tag.append(aux_tag)
tree.write(dir_output)
如果我使用“copy.deepcopy”,我没有额外的元素。输出是:
<data factor="1" name="ini" value="342" />
如果我使用“copy.copy”,只需更改元素的名称。输出是:
<data factor="1" name="raw_ini" value="342" />
知道我在做什么错吗?