使用python修改xml值文件 [英] Modify xml values file using python

查看:109
本文介绍了使用python修改xml值文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我需要修改

I'm very new to python and I need to modify the

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

<temp-config>QA</temp-config> to <temp-config>Prod</temp-config> 

使用python的所有5次出现. 不知道要使用哪个库.对此表示感谢.

for all 5 occurrences using python. Not sure what lib to use. Any help in this is really appreciated.

<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>

推荐答案

使用 lxml .此示例使用lxml.etree,并且实际上在您的示例x​​ml上失败,因为其中包含一些未关闭的标签.如果您要解析的真实数据存在相同的问题,请使用可以处理损坏的xml的lxml.html,(作为注释添加到代码中的指令).

Use lxml. This example uses lxml.etree and would actually fail on your example xml, as it has some unclosed tags in it. If you have the same problems with the real-world data that you are going to parse, use lxml.html, that can handle broken xml (instructions added to the code as comments).

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>

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

Note: As pointed out by others, you have some less powerful alternatives in the standard library. For simple tasks as this they may be well suited, however, if your xml files are broken, parsing them with standard library tools equals wasting your time.

这篇关于使用python修改xml值文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆