我有一个包含大约1000个代码的XML文件,每个代码都有以下结构:
<codes>
<code name="901" description="CONIFERADENSA BORDE" tags="Poligonos" type="1" command="" helpFile="" table="ARBOLADO" CONDITIONS="id_tipo=0122,IDIOMA=0,COMPONEN1D=2,DENSIDAD=1,TIPO_0122=1,PLANT_0122=2" enabled="1" print="1" streamMode="0">
<ccc:Triggers>
</ccc:Triggers>
<representation style="continuous" color="77" color-stereo="219" width="1" width-stereo="1" print-color="0" print-width="0.010000" font="0" italic="0" />
<io>
<iobin:transform code="901"/>
<iobind:transform code="901"/>
<iocsv:transform type="0" />
<iodgn:transform level="54" colorEntity="2" styleEntity="0" weightEntity="1" gg="901" class="0" color="2" style="0" weight="1" >
<iodgn:point type="POINT" cell="" sx="1.000000" sy="1.000000" />
<iodgn:line type="LINESTRING" />
<iodgn:text font="STANDARD" />
</iodgn:transform>
<iodwg:transform layer="901">
<iodwg:line type="Polyline3D"/>
<iodwg:point type="Point"/>
<iodwg:text style="Standard" oblique="0.000000" />
</iodwg:transform>
<ioGeomedia:transform table="CUBIERTATER_LIN" textHeight="1.000000" />
<iokml:transform type="0" elevation="0" extrude="0" opacity="255" />
<ioshp:transform nameSHP="CUBIERTATER_LIN" typeNewTable="13" />
<iosvg:transform stroke-width="1.000000" stroke="#44382b" stroke-linejoin="miter" stroke-linecap="butt" fill="0" fill-color="#44382b" point-size="1.000000" />
</io>
</code>
我需要做的是使用与code.table相同的值更改ioshp:transform.nameSHP值,因此如果示例的code.table值等于“Arbolado”,则ioshp:tansform .nameSHP也必须是“Arbolado”。
你可以帮我这个吗?我正在尝试在记事本++上使用正则表达式来使它更快,但到目前为止我无法修复......答案 0 :(得分:-1)
您需要使用re.sub regex:
>>> import re
>>> re.sub(r"(<code[^>]*table=\"(ARBOLADO)\"[^>]*>.*?<ioshp\:transform\s+nameSHP=\")(?!ARBOLADO).*?(\"[^>]*>.*?<\/code>)", r"\1\2\3", YOUR_STRING)