title: Reading XML header encoding
body: <p>I have a number of XML files I'd like to process with a script, converting them from whatever encoding that they're in to UTF-8.</p>
  
  <p>Using the code given in <a href="https://stackoverflow.com/a/191403">this great answer</a> I can do the conversion, but how can I read the encoding given in the XML header?</p>
  
  <p>For example, I have many files which are already in UTF-8, which should be left alone:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p>However, I have a lot of files which <em>do</em> need to be converted:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="windows-1255"?&gt;
  </code></pre>
  
  <p>How can I detect the XML encoding specified in the headers of these files in Python? Better, after I detect and reencode the files, how then can I change this XML header to read "utf-8" to avoid processing it in the future?</p>