title: ElementTree Replace Tag Attribute and Update File
body: <p>I am trying to clean an XML file by replacing tag attributes with the correct value.</p>
  
  <p>When I run my code below, the tag attribute is updated but only in the Element Tree tag object, the XML file is not being updated/saved.</p>
  
  <p>Is there a way to update/save the changes made to the XML objects while within ET.iterparse? Is there a way to update the file after I have changed all the tag attribute values within my loop?</p>
  
  <p>When I print out tag.attrib['v'] before and after the change, it is correctly updated with the correct value. But my saved XML file does not reflect these changes.</p>
  
  <p>All the solutions I have found do not making use of ET.iterparse method. I am working with a rather large file and would like to maintain my usage of ET.iterparse.</p>
  
  <p>Using:</p>
  
  <ul>
  <li>Python 2.7</li>
  <li>xml.etree.cElementTree</li>
  </ul>
  
  <p>Thanks.</p>
  
  <pre><code>def writeClean(self, cleaned_streets):
      '''
      Get cleaned streets mapping dictionary and use that dictionary to find
      and replace all bad street name tag attributes within XML file.
  
      Iterate through XML file to find all bad instances of tag attribute 
      street names, and replace with correct mapping value from cleaned_streets
      mapping dictionary.
  
      '''
      with open(self.getSampleFile(), 'r+') as f:            
          for event, elem in ET.iterparse(f, events=('start',)):
              if elem.tag == 'node' or elem.tag == 'way':
                  for tag in elem.iter('tag'):
                      if self.isStreetName(tag):
                          street = tag.attrib['v']
                          if street in cleaned_streets.keys():
                              tag.attrib['v'] = cleaned_streets[street]
  </code></pre>