使用python将现有的子属性值替换为子标签值

时间:2019-03-19 22:17:39

标签: python xml replace match elementtree

我希望能够解析多个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>'

1 个答案:

答案 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)