如何通过 python 替换 XML 节点

我是 python 的新手,我有一个 - 也许 - 愚蠢的 XML 文件问题(是的,我试图用谷歌搜索解决方案但没有结果)。


我必须编写一个程序来替换/切换两件事,所以首先,这是 XML 数据,它看起来像这样:


<data='qwerty'>

    <name_it>some_name</name_it>

</data>


<next_data='next_qwerty'>

    <name_it>another_name</name_it>

</next_data>


<next_next_data>

...

</next_next_data>

<next_xyz_data>...

etc.

我如何在 python 中更改some_name为data=''?所以它应该是这样的:


<data='some_name'>                            #changed from 'qwerty' to some_name

    <name_it>some_name</name_it>

</data>


<next_data='another_name'>                    #changed from 'next_qwerty' to another_name

    <name_it>another_name</name_it>

</next_data>

如果这是一个愚蠢的问题,对此感到抱歉,但我真的用谷歌搜索了它,但我找不到解决方案。


更新:这是我写的几行 python 代码:


from xml_file import data


new=""


f = io.StringIO(data)  # data loading

for r in f: 

    row = r.rstrip() 

    if 'name_it' in row: 

        change = row[row.index('name_it')] # maybe kind of len() or something

    if "<data>" in row and change: 

        idx = row.index("<data>") + 6

        new += row[:idx] + change + "name_it=\n"

        change = ""  

    else:

        new += row + "\n" # new line

这是真正的 XML 数据:


<?xml version="1.0" encoding="UTF-8"?>

<testsuite name="Setup">

    <testcase classname="Configuration" name="xxx">

        <data>abc_qwe</data>                       #change_me_to_"xxx"

    </testcase>

    <testcase classname="Configuration" name="yyy">

        <data>xyzzzz</data>                        #change_me_to_"yyy"

    </testcase>

</testsuite>

有很多迹象。名字<data>...</data>应该在name="..."



森栏
浏览 133回答 1
1回答

神不在的星期二

以下:import xml.etree.ElementTree as ETxml = '''<testsuite name="Setup">&nbsp; &nbsp; <testcase classname="Configuration" name="xxx">&nbsp; &nbsp; &nbsp; &nbsp; <data>abc_qwe</data>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </testcase>&nbsp; &nbsp; <testcase classname="Configuration" name="yyy">&nbsp; &nbsp; &nbsp; &nbsp; <data>xyzzzz</data>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; </testcase></testsuite>'''root = ET.fromstring(xml)test_cases = root.findall('.//testcase')for test_case in test_cases:&nbsp; &nbsp; test_case.find('./data').text = test_case.attrib['name']&nbsp; &nbsp;&nbsp;ET.dump(root)输出<testsuite name="Setup">&nbsp; &nbsp; <testcase classname="Configuration" name="xxx">&nbsp; &nbsp; &nbsp; &nbsp; <data>xxx</data>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </testcase>&nbsp; &nbsp; <testcase classname="Configuration" name="yyy">&nbsp; &nbsp; &nbsp; &nbsp; <data>yyy</data>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; </testcase></testsuite>另一种方式(用数据文本设置name属性的值)import xml.etree.ElementTree as ETxml = '''<testsuite name="Setup">&nbsp; &nbsp; <testcase classname="Configuration" name="xxx">&nbsp; &nbsp; &nbsp; &nbsp; <data>data_1</data>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </testcase>&nbsp; &nbsp; <testcase classname="Configuration" name="yyy">&nbsp; &nbsp; &nbsp; &nbsp; <data>data_2</data>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; </testcase></testsuite>'''root = ET.fromstring(xml)test_cases = root.findall('.//testcase')for test_case in test_cases:&nbsp; &nbsp; test_case.attrib['name'] = test_case.find('./data').text&nbsp; &nbsp;&nbsp;ET.dump(root)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python