title: ElementTree does not write data in UTF-8
body: <p>I'm pulling in data from a database and attempting to create an XML file from this data. The data is in UTF-8 and can contain characters such as <code>á</code>, <code>š</code>, or <code>č</code>. This is the code:</p>
  
  <pre><code>import xml.etree.cElementTree as ET
  
  tree = ET.parse(metadata_file)
  # ..some commands that alter the XML..
  tree.write(metadata_file, encoding="UTF-8")
  </code></pre>
  
  <p>When writing the data, the script fails with:</p>
  
  <pre><code>Traceback (most recent call last):
    File "get-data.py", line 306, in &lt;module&gt;
      main()
    File "get-data.py", line 303, in main
      tree.write(metadata_file, encoding="UTF-8")
    File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 820, in write
      serialize(write, self._root, encoding, qnames, namespaces)
    File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 939, in _serialize_xml
      _serialize_xml(write, e, encoding, qnames, None)
    File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 939, in _serialize_xml
      _serialize_xml(write, e, encoding, qnames, None)
    File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 937, in _serialize_xml
      write(_escape_cdata(text, encoding))
    File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1073, in _escape_cdata
      return text.encode(encoding, "xmlcharrefreplace")
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 32: ordinal not in range(128)
  </code></pre>
  
  <p>The only way to prevent this is to decode the data written to the XML file with:</p>
  
  <pre><code>text = text.decode('utf-8')
  </code></pre>
  
  <p>but then the resulting file will contain e.g. <code>&amp;#269;</code> rather than a <code>č</code>. Any idea how I can write the data to the file and keep it in UTF-8?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>This is the example of what the script does:</p>
  
  <pre><code>]$ echo "&lt;data&gt;&lt;/data&gt;" &gt; test.xml
  ]$ cat test.xml
  &lt;data&gt;&lt;/data&gt;
  ]$ python
  Python 2.7.5 (default, Nov  3 2014, 14:33:39)
  [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import xml.etree.cElementTree as ET
  &gt;&gt;&gt; tree = ET.parse('./test.xml')
  &gt;&gt;&gt; root = tree.getroot()
  &gt;&gt;&gt; new = ET.Element("elem")
  &gt;&gt;&gt; new.text = "á, š, or č"
  &gt;&gt;&gt; root.append(new)
  &gt;&gt;&gt; tree.write('./text.xml', encoding="UTF-8")
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 820, in write
      serialize(write, self._root, encoding, qnames, namespaces)
    File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 939, in _serialize_xml
      _serialize_xml(write, e, encoding, qnames, None)
    File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 937, in _serialize_xml
      write(_escape_cdata(text, encoding))
    File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1073, in _escape_cdata
      return text.encode(encoding, "xmlcharrefreplace")
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
  </code></pre>