我有一個XML文件,其中包含一些給定的數據。使用Python編輯XML文件中的XML文本
<?xml version="1.0" encoding="UTF-8" ?>
- <ParameterData>
<CreationInfo date="10/28/2009 03:05:14 PM" user="manoj" />
- <ParameterList count="85">
- <Parameter name="Spec 2 Included" type="boolean" mode="both">
<Value>n/a</Value>
<Result>n/a</Result>
</Parameter>
- <Parameter name="Spec 2 Label" type="string" mode="both">
<Value>n/a</Value>
<Result>n/a</Result>
</Parameter>
- <Parameter name="Spec 3 Included" type="boolean" mode="both">
<Value>n/a</Value>
<Result>n/a</Result>
</Parameter>
- <Parameter name="Spec 3 Label" type="string" mode="both">
<Value>n/a</Value>
<Result>n/a</Result>
</Parameter>
</ParameterList>
</ParameterData>
我有一個行文本文件作爲
Spec 2 Included : TRUE
Spec 2 Label: 19-Flat2-HS3
Spec 3 Included : FALSE
Spec 3 Label: 4-1-Bead1-HS3
現在我想編輯XML文本; I,E。我想用文本文件中的相應值替換字段(n/a) 。 就像我要像
<?xml version="1.0" encoding="UTF-8" ?>
- <ParameterData>
<CreationInfo date="10/28/2009 03:05:14 PM" user="manoj" />
- <ParameterList count="85">
- <Parameter name="Spec 2 Included" type="boolean" mode="both">
<Value>TRUE</Value>
<Result>TRUE</Result>
</Parameter>
- <Parameter name="Spec 2 Label" type="string" mode="both">
<Value>19-Flat2-HS3</Value>
<Result>19-Flat2-HS3</Result>
</Parameter>
- <Parameter name="Spec 3 Included" type="boolean" mode="both">
<Value>FALSE</Value>
<Result>FALSE</Result>
</Parameter>
- <Parameter name="Spec 3 Label" type="string" mode="both">
<Value>4-1-Bead1-HS3</Value>
<Result>4-1-Bead1-HS3</Result>
</Parameter>
</ParameterList>
</ParameterData>
的文件看起來我是新來這個Python的XML編碼。 我沒有關於如何編輯XML文件中的文本字段的想法。 我正在嘗試使用elementtree.ElementTree模塊。 ,但要讀取XML文件中的行,並提取屬性,我不知道哪些模塊需要導入。
請幫助。
感謝和問候。
在XML行話做到這一點,你要更改的部分被稱爲「文本」。 「屬性」是指像「name =」Spec 2標籤「或」mode =「兩者都是」。 – 2009-12-18 05:59:32
花費了相當多的時間,結合幾條建議的信息後,我寫了一個不正確但有效的解決方案:https://stackoverflow.com/questions/1591579/how-to-update -modify-A-XML的文件中的Python/48087921#48087921。也許它有助於面臨類似任務的人們。 – 2018-01-04 02:43:23