我正在嘗試對xml文件進行可變性研究。下面顯示了一個示例代碼。在創建XML文件的不同版本時僅編輯和更新XML文件中的數字文本
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<currency>1.21$/kg</currency>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<currency>4.1$/kg</currency>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
我想動態地改變(比如乘以1.5)xml中所有數字值的每個文本,例如, > 2 <或> 141100 <或和NOT> 1.21 $/kg <或>> 4.1 $/kg < <。我需要將每個文本變體另存爲一個不同的xml文件(例如,當我將列支敦士登的等級更改爲3時,將xml文件另存爲LiechtensteinRank1.5)。
我的目標是轉換每個文本輸入並保存每個更改的所有版本,然後在一起批處理後一起運行xml文件。 xml是相對較深的嵌套200,000行和多達10個不同的子節點。
我只能提取文本如下。
import xml.etree.ElementTree as ET
tree = ET.parse("Ywepr.xml")
root = tree.getroot()
for text in root.itertext():
print repr(text)
charlie = file.writelines(root.itertext())
歡迎任何建議。謝謝