title: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2
body: <p>I am creating XML file in Python and there's a field on my XML that I put the contents of a text file. I do it by</p>
  
  <pre><code>f = open ('myText.txt',"r")
  data = f.read()
  f.close()
  
  root = ET.Element("add")
  doc = ET.SubElement(root, "doc")
  
  field = ET.SubElement(doc, "field")
  field.set("name", "text")
  field.text = data
  
  tree = ET.ElementTree(root)
  tree.write("output.xml")
  </code></pre>
  
  <p>And then I get the <code>UnicodeDecodeError</code>. I already tried to put the special comment <code># -*- coding: utf-8 -*-</code> on top of my script but still got the error. Also I tried already to enforce the encoding of my variable <code>data.encode('utf-8')</code> but still got the error. I know this issue is very common but all the solutions I got from other questions didn't work for me.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Traceback: Using only the special comment on the first line of the script</p>
  
  <pre><code>Traceback (most recent call last):
    File "D:\Python\lse\createxml.py", line 151, in &lt;module&gt;
      tree.write("D:\\python\\lse\\xmls\\" + items[ctr][0] + ".xml")
    File "C:\Python27\lib\xml\etree\ElementTree.py", line 820, in write
      serialize(write, self._root, encoding, qnames, namespaces)
    File "C:\Python27\lib\xml\etree\ElementTree.py", line 939, in _serialize_xml
      _serialize_xml(write, e, encoding, qnames, None)
    File "C:\Python27\lib\xml\etree\ElementTree.py", line 939, in _serialize_xml
      _serialize_xml(write, e, encoding, qnames, None)
    File "C:\Python27\lib\xml\etree\ElementTree.py", line 937, in _serialize_xml
      write(_escape_cdata(text, encoding))
    File "C:\Python27\lib\xml\etree\ElementTree.py", line 1073, in _escape_cdata
      return text.encode(encoding, "xmlcharrefreplace")
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 243: ordina
  l not in range(128)
  </code></pre>
  
  <p>Traceback: Using <code>.encode('utf-8')</code></p>
  
  <pre><code>Traceback (most recent call last):
    File "D:\Python\lse\createxml.py", line 148, in &lt;module&gt;
      field.text = data.encode('utf-8')
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 227: ordina
  l not in range(128)
  </code></pre>
  
  <p>I used <code>.decode('utf-8')</code> and the error message didn't appear and it successfully created my XML file. But the problem is that the XML is not viewable on my browser.</p>