Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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_Xml Parsing - Fatal编程技术网

如何使用python在xml文件中插入带有属性的新标记?

如何使用python在xml文件中插入带有属性的新标记?,python,xml,xml-parsing,Python,Xml,Xml Parsing,我有一个xml文件,如下所示,我想使用python脚本在其中添加一个带有属性的标记 <package> <data> <id>sample</id> <version>1.1</version> </data> <files> <file src = "C:/sample.txt"/> </files> </package&g

我有一个xml文件,如下所示,我想使用python脚本在其中添加一个带有属性的标记

<package> 
  <data> 
    <id>sample</id> 
    <version>1.1</version> 
  </data>
  <files>
   <file src = "C:/sample.txt"/>
  </files>
</package>
我得到的输出xml是:

<package> 
  <data> 
    <id>sample</id> 
    <version>1.1</version> 
  </data>
  <files>
   <file src = "C:/sample.txt"/>
   ###i want following tag as <file src ="D:/other.txt"/>
   <file src> D:/other.txt</file src> 
  </files>
</package>

样品
1.1
###我想要下面的标签
D:/other.txt

因此,请建议如何在xml文件中添加带有属性的标记?

您可以使用
ET.Element('file',{'src':'D:/other.txt')
来构造名为
file
的元素,该元素的属性名为
src

<package> 
  <data> 
    <id>sample</id> 
    <version>1.1</version> 
  </data>
  <files>
   <file src = "C:/sample.txt"/>
   ###i want following tag as <file src ="D:/other.txt"/>
   <file src> D:/other.txt</file src> 
  </files>
</package>