我希望能够解析多个ReportCharacters
,并确保所有人都使用characterID
name属性中的ReportCharacter
数字而不是实际的字符名称。
如何解析和比较ReportCharacter name
,如果CharacterID
不存在,则用相应的CharacterID
值替换名称?
这是一些示例原始文件:
' <?xml version="1.0" ?>
<CharacterData>
<Report>
<ReportCharacter name="scoobydoo">
<CharacterProperties>
<tag name="charactername">scoobydoo</tag>
<tag name="characterID">1024724312</tag>
<tag name="characterSTART">Sat Sep 15 02:01:56 2018</tag>
</CharacterProperties>
</ReportCharacter>
<ReportCharacter name="mickey">
<CharacterProperties>
<tag name="charactername">mickey</tag>
<tag name="characterID">1024724313</tag>
<tag name="characterSTART">Sat Sep 15 02:01:56 2018</tag>
</CharacterProperties>
</ReportCharacter>
<ReportCharacter name="goofy">
<CharacterProperties>
<tag name="charactername">goofy</tag>
<tag name="characterID">1024724314</tag>
<tag name="characterSTART">Sat Sep 15 02:01:56 2018</tag>
</CharacterProperties>
</ReportCharacter>
</Report>
</CharacterData>'
这就是我希望修改后的文件的样子:
' <?xml version="1.0" ?>
<CharacterData>
<Report>
<ReportCharacter name="1024724312">
<CharacterProperties>
<tag name="charactername">scoobydoo</tag>
<tag name="characterID">1024724312</tag>
<tag name="characterSTART">Sat Sep 15 02:01:56 2018</tag>
</CharacterProperties>
</ReportCharacter>
<ReportCharacter name="1024724313">
<CharacterProperties>
<tag name="charactername">mickey</tag>
<tag name="characterID">1024724313</tag>
<tag name="characterSTART">Sat Sep 15 02:01:56 2018</tag>
</CharacterProperties>
</ReportCharacter>
<ReportCharacter name="1024724314">
<CharacterProperties>
<tag name="charactername">goofy</tag>
<tag name="characterID">1024724314</tag>
<tag name="characterSTART">Sat Sep 15 02:01:56 2018</tag>
</CharacterProperties>
</ReportCharacter>
</Report>
</CharacterData>'
答案 0 :(得分:0)
import xml.etree.ElementTree as ET
tree = ET.parse('characters.txt')
root = tree.getroot()
for character in root.iter('ReportCharacter'):
current_name = character.get('name', '')
if current_name.isdecimal():
continue
name = character.findtext('.//tag[@name="characterID"]')
if name:
character.set('name', name)
with open('characters_parsed.xml', 'w') as file_out:
tree.write(file_out, encoding='unicode', short_empty_elements=True)