4

私はPythonに非常に慣れていないので、変更する必要があります

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

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

pythonを使用して5回すべて発生します。使用するライブラリがわからない。これで何か助けていただければ幸いです。

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

4 に答える 4

2

ElementTreeは素晴らしい選択です。純粋な Python であり、標準ライブラリに含まれているため、最も移植性の高いオプションです。ただし、私はいつもまっすぐに進みlxmlます - それは同じ API を持っており、より高速で、より多くのことができます (事実上ラッパーであるためlibxml2)。

from lxml import etree
tree = etree.parse(path_to_my_xml)

for elem in tree.findall('.//test'):
    assert elem.attrib['name'] == 'test02'
    elem.attrib['name'] == 'test03'

for elem in tree.findall('.//temp-config'):
    assert elem.text == 'QA'
    elem.text = 'Prod'

with open(path_to_output_file, 'w') as file_handle:
    file_handle.write(etree.tostring(tree, pretty_print=True, encoding='utf8'))
于 2013-01-28T18:37:29.433 に答える
2

lxmlを使用します。この例ではlxml.etree 、いくつかの閉じられていないタグが含まれているため、例の xml を使用していますが、実際には失敗します。解析しようとしている実世界のデータに同じ問題がある場合は、lxml.html,破損した xml を処理できるものを使用してください (命令はコメントとしてコードに追加されます)。

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 ファイルが壊れている場合、標準ライブラリ ツールでそれらを解析することは時間を無駄にすることになります。

于 2013-01-28T18:38:32.220 に答える
0

ElementTree を使用することをお勧めします: http://docs.python.org/2/library/xml.etree.elementtree.html

例:

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

または、このスレッドを参照してください: How do I parse XML in Python?

于 2013-01-28T18:31:54.673 に答える
0

上記の回答を完了するには、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'
于 2013-01-28T19:16:49.037 に答える