Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python更新xml文件?_Python_Xml - Fatal编程技术网

使用python更新xml文件?

使用python更新xml文件?,python,xml,Python,Xml,我有一个python脚本,它从SQL查询返回元组列表。我想用列表中的数据更新一个xml文件 我能做些什么,用我从查询中收到的列表只写一个特定的元素或节点?多谢各位 好的,我可以使用ElementTree中的findall()找到元素列表,有人建议我使用setAttribute进行更新,这会起作用。但是,对于使用setAttributes插入重复数据的可能性,我该怎么办 假设我的xml包含一个客户列表: (客户名称=“鲍勃”,年龄=“22”) 我可以使用循环遍历SQL数据,并使用setAttri

我有一个python脚本,它从SQL查询返回元组列表。我想用列表中的数据更新一个xml文件

我能做些什么,用我从查询中收到的列表只写一个特定的元素或节点?多谢各位

好的,我可以使用ElementTree中的findall()找到元素列表,有人建议我使用setAttribute进行更新,这会起作用。但是,对于使用setAttributes插入重复数据的可能性,我该怎么办

假设我的xml包含一个客户列表:

(客户名称=“鲍勃”,年龄=“22”)

我可以使用循环遍历SQL数据,并使用setAttribute添加新数据。但是,如果我的sql数据恰好包含一个额外的(customer name=“bob”,age=“22”)数据,这难道不会添加到列表中吗?谢谢。

使用标准Python库中的模块。您可以读取XML文件,将其解析为树,然后找到要查找的元素,修改它们,然后写回

如果您有更具体的问题,请提问。





您可以使用
Element.setAttribute(名称、值)、Element.setAttributeNode(newAttr)
等进行更新

谢谢。我可以使用ElementTree文档在xml文档中查找元素。我只是对更新一般概念有一个问题。例如,我的sql查询返回以下集合:,,,@timtran:您可以删除现有元素,然后添加新元素,或者更改现有元素元素-无论哪种方式更适合您谢谢。我可以使用ElementTree文档在xml文档中查找元素。我只是对更新一般概念有一个问题。例如,我的sql查询返回以下集合:(tom,22),(mary,23),(james 19),(ed,37),(paul,28)。我想用此数据更新我的xml文件。xml已包含,。为防止重复,我是否只需删除所有客户标记,然后创建新的元素列表以将其添加回?