title: How to Convert Extended ASCII to HTML Entity Names in Python?
body: <p>I'm currently doing this to replace extended-ascii characters with their HTML-entity-number equivalents:</p>
  
  <pre><code>s.encode('ascii', 'xmlcharrefreplace')
  </code></pre>
  
  <p>What I would like to do is convert to the HTML-entity-name equivalent (i.e. <code>&amp;copy;</code> instead of <code>&amp;#169;</code>).  This small program below shows what I'm trying to do that is failing. Is there a way to do this, aside from doing a find/replace?</p>
  
  <pre><code>#coding=latin-1
  
  def convertEntities(s):
      return s.encode('ascii', 'xmlcharrefreplace')
  
  ok = 'ascii: !@#$%^&amp;*()&lt;&gt;'
  not_ok = u'extended-ascii: ©®°±¼'
  
  ok_expected = ok
  not_ok_expected = u'extended-ascii: &amp;copy;&amp;reg;&amp;deg;&amp;plusmn;&amp;frac14;'
  
  ok_2 = convertEntities(ok)
  not_ok_2 = convertEntities(not_ok)
  
  if ok_2 == ok_expected:
      print 'ascii worked'
  else:
      print 'ascii failed: "%s"' % ok_2
  
  if not_ok_2 == not_ok_expected:
      print 'extended-ascii worked'
  else:
      print 'extended-ascii failed: "%s"' % not_ok_2
  </code></pre>