Py学习  »  Python

在xml文件中找到一行,并使用python将下一行替换为其他内容

moong • 7 年前 • 1994 次点击  

使用python,当上面的行匹配时,我需要替换xml文件中的行。

    <groupId>test</groupId>
    <artifactId>dmrfolder</artifactId>
    <version>1.0</version>

我想在这里搜索 dmrfolder 如果找到了,我需要更换下面的 1.0 具有 somevalue .

我按下面的方法走,但对我不起作用。

with open('test.xml', 'r') as file :
            data = file.read()
            fnl = data.replace(line[1], str)
            with open('test.xml', 'w') as fw:
               fw.write(fnl)
               fw.close()

预期的结果是

    <groupId>test</groupId>
    <artifactId>dmrfolder</artifactId>
    <version>somevalue</version>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44249
文章 [ 2 ]  |  最新文章 7 年前