Reading and writing unique XML tags in Python
Question: I want to take a .txt file in XML of this sort:

    
    
    <?xml version = ""?>
    <data>
        <a1>cat</a1>
        <a5>bird</a5>
        <a4>window</a4>
    </data>
    

count the length of each string and output:

    
    
    <?xml version = ""?>
    <result>
        <r1>3</r1>
        <r5>4</r5>
        <r4>6</r4>
    </result>
    

What's the best way to write a .txt xml format file with the above output and
corresponding tags? I'm using `xml.etree.cElementTree` to parse it.


Answer: 
    import xml.etree.ElementTree as ET
    import re
    xdata = '''
    <data>
        <a1>cat</a1>
        <a5>bird</a5>
        <a4>window</a4>
    </data>'''
    root = ET.fromstring(xdata)
    
    for apptag in root.findall("*"):
        apptag.text = str(len(apptag.text))
        apptag.tag = re.sub(r'^a(.*)',r'r\1',apptag.tag)
    root.tag = 'result'
    ET.ElementTree(root).write('test.xhtml')
    

