我正在嘗試替換<color></color>KML 檔案中子標簽值的前六個字符:
當前的:
<Style id='1'>
<LineStyle>
<color>9900e6ff</color>
<width>2</width>
</LineStyle>
預期的:
<Style id='1'>
<LineStyle>
<color>ffffffff</color>
<width>2</width>
</LineStyle>
嘗試的sed代碼:
sed -i s/<LineStyle><color>[0-9]*</color></LineStyle>/<LineStyle><color>ffffff[0-9][a-z]</color></LineStyle>/g' file.kml
uj5u.com熱心網友回復:
我建議其他評論員不要使用正則運算式進行元素搜索,因為可能出現不可預測的行為,最好選擇任何帶有xpath的包,例如lxmlor xml。
from lxml import etree as ET
some_xml = """
<Style id='1'>
<LineStyle>
<color>9900e6ff</color>
<width>2</width>
</LineStyle>
</Style>
"""
some_xml = ET.fromstring(some_xml)
if len(result := some_xml.xpath("descendant::color")) == 1:
result[0].text = "ffffffff"
print(ET.tostring(some_xml, encoding="unicode"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/497153.html
上一篇:將Matlab轉換為python代碼:IndexError:index-1isoutofboundsforaxis0withsize0
下一篇:在linux中記錄已洗掉的檔案
