Python:从XML文件删除行并创建一个新的xml文件

时间:2019-04-23 19:49:25

标签: xml parsing python-3.6

我有一个XML文件链接:https://drive.google.com/file/d/1G0qtDY6HY9x7HQLY3vEmlba8cxLym5sw/view?usp=sharing

我要删除X =“-3.276700000000E + 04”和Y =“-3.276700000000E + 04”的行。该块如下所示:

<GCPList Projection="GEOGCS[&quot;WGS 
84&quot;,DATUM[&quot;WGS_1984&quot;,SPHEROID[&quot;WGS 84&quot;,6378137,298.257223563,AUTHORITY[&quot;EPSG&quot;,&quot;7030&quot;]],AUTHORITY[&quot;EPSG&quot;,&quot;6326&quot;]],PRIMEM[&quot;Greenwich&quot;,0,AUTHORITY[&quot;EPSG&quot;,&quot;8901&quot;]],UNIT[&quot;degree&quot;,0.0174532925199433,AUTHORITY[&quot;EPSG&quot;,&quot;9122&quot;]],AUTHORITY[&quot;EPSG&quot;,&quot;4326&quot;]]">
    <GCP Id="" Pixel="0.5000" Line="0.5000" X="-8.887793731689E+01" Y="4.566011428833E+01" />
    <GCP Id="" Pixel="260.5000" Line="0.5000" X="-8.877786254883E+01" Y="4.566171646118E+01" />
    <GCP Id="" Pixel="520.5000" Line="0.5000" X="-8.867778015137E+01" Y="4.566323089600E+01" />...
<GCP Id="" Pixel="3640.5000" Line="7347.5000" X="-3.276700000000E+04" Y="-3.276700000000E+04" />
    <GCP Id="" Pixel="3900.5000" Line="7347.5000" X="-3.276700000000E+04" Y="-3.276700000000E+04" />
    <GCP Id="" Pixel="4160.5000" Line="7347.5000" X="-3.276700000000E+04" Y="-3.276700000000E+04" />
    <GCP Id="" Pixel="4420.5000" Line="7347.5000" X="-3.276700000000E+04" Y="-3.276700000000E+04" />
</GCPList>

我想删除这些代码行并更新文件。

我写的代码是:

file = ET.parse('input6.vrt')
root = file.getroot()
for item in root.findall('.//trigger/GCPList'):
    check_req_elems = elem.find('./@X=-32767]' and './@Y=-32767')
    check_elem = elem.find('./@X=-32767]' and './@Y=-32767')
    if check_req_elems is not None and check_elem is not None:
            elem.remove(check_elem)

但是,这似乎并未对该文件进行任何更改。

0 个答案:

没有答案