Set attribute to Element in Python
Question: I am using ElementTree to build an xml. But I am getting an error at Line no:
5

AttributeError: **setattr**

    
    
    1.import xml.etree.cElementTree as ET
    2.summary = open(Summary.xml, 'w')
    3.root = ET.Element('Summary')
    4.ET.SubElement(root, 'TextSummary')
    5.ET.SubElement(root,'TextSummary').__setattr__('Status','Completed')
    6.tree = ET.ElementTree(root) 
    7.tree.write(summary)
    8.summary.close()
    

After code execution, my xml should look like mentioned below.

    
    
    < Summary>
        < TextSummary Status = 'Completed'/>
    < /Summary>
    

How to add attributes to Elements in xml using python
**_xml.etree.cElementTree_**


Answer: You should be doing:

    
    
    ET.SubElement(root,'TextSummary').set('Status','Completed')
    

The [Etree documentation shows
usage](http://docs.python.org/2/library/xml.etree.elementtree.html#modifying-
an-xml-file).

