title: Python throws UnicodeEncodeError although I am doing str.decode(). Why?
body: <p>Consider this function:</p>
  
  <pre><code>def escape(text):
      print repr(text)
      escaped_chars = []
      for c in text:
          try:
              c = c.decode('ascii')
          except UnicodeDecodeError:
              c = '&amp;{};'.format(htmlentitydefs.codepoint2name[ord(c)])
          escaped_chars.append(c)
      return ''.join(escaped_chars)
  </code></pre>
  
  <p>It should escape all non ascii characters by the corresponding htmlentitydefs. Unfortunately python throws</p>
  
  <pre><code>UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 0: ordinal not in range(128)
  </code></pre>
  
  <p>when the variable <code>text</code> contains the string whose <code>repr()</code> is <code>u'Tam\xe1s Horv\xe1th'</code>.</p>
  
  <p>But, I don't use <code>str.encode()</code>. I only use <code>str.decode()</code>. Do I miss something?</p>