title: Python: ascii codec can't encode en-dash
body: <p>I'm trying to print a poem from the Poetry Foundation's daily poem RSS feed with a <a href="https://www.adafruit.com/products/597" rel="nofollow">thermal printer</a> that supports an encoding of CP437. This means I need to translate some characters; in this case an en-dash to a hyphen. But python won't even encode the en dash to begin with. When I try to decode the string and replace the en-dash with a hyphen I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "pftest.py", line 46, in &lt;module&gt;
      str = str.decode('utf-8')
    File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
      return codecs.utf_8_decode(input, errors, True)
  UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 140: ordinal not in range(128)
  </code></pre>
  
  <p>And here is my code:</p>
  
  <pre><code>#!/usr/bin/python
  #-*- coding: utf-8 -*-
  
  # This string is actually a variable entitled d['entries'][1].summary_detail.value
  str = """Love brought by night a vision to my bed,
  One that still wore the vesture of a child
  But eighteen years of age – who sweetly smiled"""
  
  str = str.decode('utf-8')
  str = str.replace("\u2013", "-") #en dash
  str = str.replace("\u2014", "--") #em dash
  print (str)
  </code></pre>
  
  <p>I can actually print the output using the following code without errors in my terminal window (Mac), but my printer spits out sets of 3 CP437 characters:</p>
  
  <pre><code>str = u''.str.encode('utf-8')
  </code></pre>
  
  <p>I'm using Sublime Text as my editor, and I've saved the page with UTF-8 encoding, but I'm not sure that will help things. I would greatly appreciate any help with this code. Thank you!</p>