title: UnicodeEncodeError: 'ascii' codec can't encode character [...]
body: <p>I have read the <a href="http://docs.python.org/howto/unicode.html">HOWTO on Unicode</a> from the official docs and a full, very detailed <a href="http://boodebr.org/main/python/all-about-python-and-unicode">article</a> as well. Still I don't get it why it throws me this error.</p>
  
  <p>Here is what I attempt: I open an XML file that contains chars out of ASCII range (but inside allowed XML range). I do that with <code>cfg = codecs.open(filename, encoding='utf-8, mode='r')</code> which runs fine. Looking at the string with <code>repr()</code> also shows me a unicode string.</p>
  
  <p>Now I go ahead and read that with <code>parseString(cfg.read().encode('utf-8')</code>. Of course, my XML file starts with this: <code>&lt;?xml version="1.0" encoding="utf-8"?&gt;</code>. Although I suppose it is not relevant, I also defined utf-8 for my python script, but since I am not writing unicode characters directly in it, this should not apply here. Same for the following line: <code>from __future__ import unicode_literals</code> which also is right at the beginning.</p>
  
  <p>Next thing I pass the generated Object to my own class where I read tags into variables like this: <code>xmldata.getElementsByTagName(tagName)[0].firstChild.data</code> and assign it to a variable in my class.</p>
  
  <p>Now what perfectly works are those commands (obj is an instance of the class):</p>
  
  <pre><code>for element in obj:
      print element
  </code></pre>
  
  <p>And this command does work as well:</p>
  
  <pre><code>print obj.__repr__()
  </code></pre>
  
  <p>I defined <code>__iter__()</code> to just yield every variable while <code>__repr__()</code> uses the typical printf stuff: <code>"%s" % self.varname</code></p>
  
  <p>Both commands print perfectly and can output the unicode character.
  What does not work is this:</p>
  
  <pre><code>print obj
  </code></pre>
  
  <p>And now I am stuck because this throws the dreaded</p>
  
  <pre><code>UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 47:
  </code></pre>
  
  <p>So what am I missing? What am I doing wrong? I am looking for a general solution, I always want to handle strings as unicode, just to avoid any possible errors and write a compatible program.</p>
  
  <p>Edit: I also defined this:</p>
  
  <pre><code>def __str__(self):
      return self.__repr__()
  def __unicode__(self):
      return self.__repr__()
  </code></pre>
  
  <p>From documentation I got that this </p>