Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找并替换elementtree中的文本_Python_Elementtree - Fatal编程技术网

Python 查找并替换elementtree中的文本

Python 查找并替换elementtree中的文本,python,elementtree,Python,Elementtree,我对编程和python非常陌生。我正在尝试查找并替换xml文件中的文本。这是我的xml文件 <?xml version="1.0" encoding="UTF-8"?> <!--Arbortext, Inc., 1988-2008, v.4002--> <!DOCTYPE doc PUBLIC "-//MYCOMPANY//DTD XSEIF 1/FAD 110 05 R5//EN" "XSEIF_R5.dtd"> <doc version="XSEI

我对编程和python非常陌生。我正在尝试查找并替换xml文件中的文本。这是我的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!--Arbortext, Inc., 1988-2008, v.4002-->
<!DOCTYPE doc PUBLIC "-//MYCOMPANY//DTD XSEIF 1/FAD 110 05 R5//EN"
 "XSEIF_R5.dtd">
<doc version="XSEIF R5"
xmlns="urn:x-mycompany:r2:reg-doc:1551-fad.110.05:en:*">
<meta-data></meta-data>
<front></front> 
<body>
<chl1><title xml:id="id_881i">Installation</title>
<p>To install SDK, perform the tasks mentioned in the following
table.</p>
<p><input>ln -s /sim/<var>user_id</var>/.VirtualBox $home/.VirtualBox</input
></p>
</chl1>
</body>
</doc>
 <?Pub *0000021917 0?>

文本可以在body下的许多子标签中找到。我得到了删除子元素并添加新元素的方法,但没有得到只替换文本的方法

替换文本、尾部属性

import lxml.etree as ET

with open('1.xml', 'rb+') as f:
    tree = ET.parse(f)
    root = tree.getroot()
    for elem in root.getiterator():
        if elem.text:
            elem.text = elem.text.replace('VirtualBox', 'Xen')
        if elem.tail:
            elem.tail = elem.tail.replace('VirtualBox', 'Xen')

    f.seek(0)
    f.write(ET.tostring(tree, encoding='UTF-8', xml_declaration=True))
    f.truncate()

替换文本、尾部属性

import lxml.etree as ET

with open('1.xml', 'rb+') as f:
    tree = ET.parse(f)
    root = tree.getroot()
    for elem in root.getiterator():
        if elem.text:
            elem.text = elem.text.replace('VirtualBox', 'Xen')
        if elem.tail:
            elem.tail = elem.tail.replace('VirtualBox', 'Xen')

    f.seek(0)
    f.write(ET.tostring(tree, encoding='UTF-8', xml_declaration=True))
    f.truncate()

可能最简单的方法是:

ifile = open('input_file','r')
ofile = open('output_file','w')
for line in ifile.readlines():
  ofile.write(line.replace('VirtualBox','Xen'))
ifile.close()
ofile.close()

可能最简单的方法是:

ifile = open('input_file','r')
ofile = open('output_file','w')
for line in ifile.readlines():
  ofile.write(line.replace('VirtualBox','Xen'))
ifile.close()
ofile.close()

嗨,谢谢你的回答。但它不会在原始文件中抛出任何错误或替换。我对发生的事感到困惑。甚至另一个剧本也是如此。给你。从xml.etree.ElementTree导入元素tree=ET.parse('C:/Users/epeeham/Desktop/Official/SSR-SIM/Test/1_1531-CRA 119 1364_2.xml')doc=tree.getroot()elem=Element(“approved by”)elem.attrib[“approved”]=“yes”我尝试将属性值从否设置为是。但是它没有给我一个错误,也没有起作用。我的xml有问题吗?@ShahulHameed,我更新了代码。更新后的代码使用外部库。您好,谢谢您的回答。但它不会在原始文件中抛出任何错误或替换。我对发生的事感到困惑。甚至另一个剧本也是如此。给你。从xml.etree.ElementTree导入元素tree=ET.parse('C:/Users/epeeham/Desktop/Official/SSR-SIM/Test/1_1531-CRA 119 1364_2.xml')doc=tree.getroot()elem=Element(“approved by”)elem.attrib[“approved”]=“yes”我尝试将属性值从否设置为是。但是它没有给我一个错误,也没有起作用。我的xml有问题吗?@ShahulHameed,我更新了代码。更新后的代码使用外部库。您好,谢谢您的回答。在ElementTree方法之前我尝试过这种方法。但是我的xml文件被完全删除了。内容变为空。不知道如果你有读取输入文件和写入输出文件的正确权限,它必须工作。我的文件一定和我的不同。嗨,谢谢你的回答。在ElementTree方法之前我尝试过这种方法。但是我的xml文件被完全删除了。内容变为空。不知道如果你有读取输入文件和写入输出文件的正确权限,它必须工作。ifile必须与ofile不同。