使用python修改xml值文件

使用python修改xml值文件,python,xml,parsing,Python,Xml,Parsing,我对python非常陌生,需要修改 <test name="test02"></xmpp> to <test name="test03"></xmpp> <temp-config>QA</temp-config> to <temp-config>Prod</temp-config> 到 产品质量保证 对于所有5个使用python的实例。 不确定要使用什么库。在这方面的任何帮助都是非常感谢的

我对python非常陌生,需要修改

<test name="test02"></xmpp> to <test name="test03"></xmpp> 

<temp-config>QA</temp-config> to <temp-config>Prod</temp-config> 
到
产品质量保证
对于所有5个使用python的实例。 不确定要使用什么库。在这方面的任何帮助都是非常感谢的

<config>
<logging></logging>
<test-mode>false</test-mode>
<test name="test02"></xmpp>
<mail></mail>
<test-system>0</test-system>
<system id="0" name="suite1" type="regression">
    <temp-config>QA</temp-config>
    <rpm>0.5</rpm>
    <cycles>3</cycles>
</system>
<system id="1" name="suite2" type="regression">
    <temp-config>QA</temp-config>
    <rpm>0.5</rpm>
    <cycles>3</cycles>
</system>
<system id="2" name="suite3" type="regression">
    <temp-config>QA</temp-config>
    <rpm>0.5</rpm>
    <cycles>3</cycles>
</system>
<system id="3" name="suite4" type="regression">
    <temp-config>QA</temp-config>
    <rpm>0.5</rpm>
    <cycles>3</cycles>
</system>
<system id="4" name="suite5" type="regression">
    <temp-config>QA</temp-config>
    <rpm>0.5</rpm>
    <cycles>3</cycles>
</system>
</config>

假的
0
质量保证
0.5
3.
质量保证
0.5
3.
质量保证
0.5
3.
质量保证
0.5
3.
质量保证
0.5
3.

我建议使用ElementTree:

例如:

for atype in e.findall('type')
 print(atype.get('foobar'))

或者看看这个帖子:

ElementTree
是一个很好的选择——纯Python并包含在标准库中,因此它是最可移植的选项。但是,我总是直接使用
lxml
——它有相同的API,速度更快,而且可以做得更多(因为它实际上是
libxml2
的包装器)

使用。本例使用了
lxml.etree
,但在示例xml中实际会失败,因为其中有一些未关闭的标记。如果您对要解析的实际数据有相同的问题,请使用
lxml.html、
来处理损坏的xml(作为注释添加到代码中的说明)

[14]中的
:将lxml.etree作为et导入#对于损坏的xml,添加导入:
#将lxml.html作为lh导入
在[15]中:doc=et.fromstring(xmlstr)#对于断开的xml,将此行替换为:
#doc=lh.fromstring(xmlstr)
#如果从文件中读取xml:
#doc=et.parse('文件\路径')
在[16]中:对于doc.xpath('.//temp config')中的元素:
…:elem.text='Prod'
...:     
在[17]:print et.tostring(doc,pretty\u print=True)
假的
0
戳
0.5
3.
戳
0.5
3.
戳
0.5
3.
戳
0.5
3.
戳
0.5
3.

注意:正如其他人指出的,标准库中有一些功能较弱的替代方案。对于这样的简单任务,它们可能非常适合,但是,如果您的xml文件被破坏,那么使用标准库工具解析它们就等于浪费您的时间。

要使用lxml完成上述答案,请使用以下方法更改“name”属性值:

from lxml import etree
tree = etree.parse(path_to_my_xml)
for elem in tree.xpath('.//temp-config'):
    elem.text = 'Prod'
for elem in tree.xpath(".//test[@name='test02']"):
    elem.attrib['name'] = 'test03'

抱歉,我收到以下错误:回溯(最近一次调用上次):文件“testrun.py”,第13行,以open(tree,'w')作为文件\u句柄:TypeError:强制使用Unicode:需要字符串或缓冲区,lxml.etree.\u ElementTreefound@DigitalDyn-
打开(树“w”)作为文件句柄:
不是我发布的内容的一部分。我想您可能已经犯了一个复制粘贴错误:)@DigitalDyn
将open(file_out,'wb')作为文件句柄:
In [14]: import lxml.etree as et  # for broken xml add an import:
                                  # import lxml.html as lh

In [15]: doc = et.fromstring(xmlstr)  # for broken xml replace this line with:
                                      # doc = lh.fromstring(xmlstr)

                                      # if you read xml from a file:
                                      # doc = et.parse('file_path')

In [16]: for elem in doc.xpath('.//temp-config'):
    ...:     elem.text = 'Prod'
    ...:     

In [17]: print et.tostring(doc,pretty_print=True)
<config>
  <logging/>
  <test-mode>false</test-mode>
  <test name="test02">
    <mail/>
    <test-system>0</test-system>
    <system id="0" name="suite1" type="regression">
      <temp-config>Prod</temp-config>
      <rpm>0.5</rpm>
      <cycles>3</cycles>
    </system>
    <system id="1" name="suite2" type="regression">
      <temp-config>Prod</temp-config>
      <rpm>0.5</rpm>
      <cycles>3</cycles>
    </system>
    <system id="2" name="suite3" type="regression">
      <temp-config>Prod</temp-config>
      <rpm>0.5</rpm>
      <cycles>3</cycles>
    </system>
    <system id="3" name="suite4" type="regression">
      <temp-config>Prod</temp-config>
      <rpm>0.5</rpm>
      <cycles>3</cycles>
    </system>
    <system id="4" name="suite5" type="regression">
      <temp-config>Prod</temp-config>
      <rpm>0.5</rpm>
      <cycles>3</cycles>
    </system>
  </test>
</config>
from lxml import etree
tree = etree.parse(path_to_my_xml)
for elem in tree.xpath('.//temp-config'):
    elem.text = 'Prod'
for elem in tree.xpath(".//test[@name='test02']"):
    elem.attrib['name'] = 'test03'