title: Getting Unicode Error when looping through tags and writing XML
body: <p>I am trying to write out some XML that does have some special characters.  The place where I am running in trouble is when I iterate through a list of tags to create several elements called tag.</p>
  
  <pre><code># -*- coding: utf-8 -*-
  import xml.etree.ElementTree as xml
  
  reload(sys)
  sys.setdefaultencoding('utf-8')
  </code></pre>
  
  <p>Snippet of code:</p>
  
  <pre><code>    check = (video['tags'].split(', '))
      x=len(check)
      y=x-1
      for i in xrange(0,y):
          tagger = xml.SubElement(doc, 'field', name="tag")
          s=check[i]
          tagger.text = s.encode('utf-8')
  </code></pre>
  
  <p>The problem is when I try to write:</p>
  
  <pre><code>output = open(file_name,'w+')
  tree = xml.ElementTree(add)
  tree.write(output)
  output.close()
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "xml_breakup3.py", line 108, in &lt;module&gt;
      tagger.text = s.encode('utf-8')
  UnicodeDecodeError: 'utf8' codec can't decode byte 0x81 in position 0: invalid start byte
  </code></pre>
  
  <p>When I run my code without this snippet it writes the xml without a problem.  If I make tagger.text = any kind of string(i.e. '99') it writes fine. If I make the loop go from 0 to 3 it works.  It is only when I try to iterate through the whole list that I get an UnicodeDecode Error</p>
  
  <p>when I try:</p>
  
  <pre><code>    check = (video['tags'].split(', '))
      for ta in check:
          tagger = xml.SubElement(doc, 'field', name="tag")
          tagger.text = ta
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>     Traceback (most recent call last):
         File "xml_breakup3.py", line 172, in &lt;module&gt;
           tree.write(output)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 821, in write
      serialize(write, self._root, encoding, qnames, namespaces)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 940, in _serialize_xml
       _serialize_xml(write, e, encoding, qnames, None)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 940, in _serialize_xml
      _serialize_xml(write, e, encoding, qnames, None)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 938, in _serialize_xml
      write(_escape_cdata(text, encoding))
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1074, in _escape_cdata
      return text.encode(encoding, "xmlcharrefreplace")
  </code></pre>
  
  <p>UnicodeDecodeError: 'utf8' codec can't decode byte 0xba in position 0: invalid start byte</p>