title: Python xml - handle unclosed token
body: <p>I am reading in hundreds of XML files and parsing them with xml.etree.ElementTree.  </p>
  
  <p>Quick background just fwiw:
  These XML files were at one point totally valid but somehow when processing them historically my process which copied/pasted them may have corrupted them.  (Turns out it was a flushing issue / with statement not closing, if you care, see the good help I got on that investigation at...  <a href="https://stackoverflow.com/questions/31546902/python-shutil-copyfile-missing-last-few-lines">Python shutil copyfile - missing last few lines</a> ).  </p>
  
  <p>Anyway back to the point of this question.<br>
  I would still like to read in the first 100,000 lines or so of these documents which are valid XML.  The files are only missing the last 4 or 5KB of a 6MB file.  As alluded to earlier, though, the file just 'cuts out'.  it looks like this:</p>
  
  <pre><code>&lt;/Maintag&gt;
  
  
  
  
  &lt;Maintag&gt;
      &lt;Change_type&gt;NQ&lt;/Change_type&gt;
      &lt;Name&gt;Atlas&lt;/Name&gt;
      &lt;Test&gt;ATLS&lt;/Test&gt;
      &lt;Other&gt;NYSE&lt;/Other&gt;
      &lt;Scheduled_E
  </code></pre>
  
  <p>where (perhaps obviously) Scheduled_E is the beginning of what should be another attribute, &lt;.Scheduled_Event>, say.  But the file gets cut short mid tag.  Once again, before this point in the file, there are several thousand 'good' "Maintag" entries which I would like to read in, accepting the cutoff entry (and obviously anything that should have come after) as an unrecoverable fail.   </p>
  
  <p>A simple but incomplete method of dealing with this might be to simply - pre XML processing - look for the last instance of the string &lt;./Maintag> in the file, and replace what follows (which will be broken, at some point) with the 'opening' tags.  Again, this at least lets me process what is still there and valid.</p>
  
  <p>If someone wants to help me out with that sort of string replacement, then fwiw the opening tags are:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
  &lt;FirstTag&gt;
      &lt;Source FileName="myfile"&gt;
  </code></pre>
  
  <p>I am hoping that even easier than that, there might be an elementtree or beautifulsoup or other way of handling this situation... I've done a decent amount of searching and nothing seems easy/obvious.</p>
  
  <p>Thanks</p>