title: UnicodeEncodeError: 'ascii' codec can't encode character in position 0: ordinal not in range(128)
body: <p>I'm working on a Python script that uses the scissor character (9986 - ✂) and I'm trying to port my code to Mac, but I'm running into this error.</p>
  
  <p>The scissor character shows up fine when run from IDLE (Python 3.2.5 - OS X 10.4.11 iBook G4 PPC) and the code works entirely fine on Ubuntu 13.10, but when I attempt to run this in the terminal I get this error/traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "snippets-convert.py", line 352, in &lt;module&gt;
      main()
    File "snippets-convert.py", line 41, in main
      menu()
    File "snippets-convert.py", line 47, in menu
      print ("|\t ",snipper.decode(),"PySnipt'd",snipper.decode(),"\t|")
  UnicodeEncodeError: 'ascii' codec can't encode character '\u2702' in position 0: ordinal not in range(128)
  </code></pre>
  
  <p>and the code that is giving me the problem:</p>
  
  <p><code>print ("|\t ",chr(9986),"PySnipt'd",chr(9986),"\t|")</code> </p>
  
  <p>Doesn't this signal that the terminal doesn't have the capability to display that character? I know this is an old system, but it is currently the only system I have to use. Could the age of the OS is interfering with the program?</p>
  
  <p>I've read over these questions:</p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/5141559/unicodeencodeerror-ascii-codec-cant-encode-character-u-xef-in-position-0">UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode character u&#39;\xef&#39; in position 0: ordinal not in range(128)</a> - Different character</p></li>
  <li><p><a href="https://stackoverflow.com/questions/1652904/unicodeencodeerror-ascii-codec-cant-encode-character">&quot;UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode character&quot;</a> - Using 2.6, so don't know if it applies</p></li>
  <li><p><a href="https://stackoverflow.com/questions/1653569/unicodeencodeerror-ascii-codec-cant-encode-character">UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode character?</a> - Seems to be a plausible solution to my problem, <code>.encode('UTF-8')</code>, I don't get the error. However, it displays a character code, not the character I want, and <code>.decode()</code> just gives me the same error. Not sure if I'm doing this right.</p></li>
  <li><p><a href="https://stackoverflow.com/questions/11865582/unicodeencodeerror-ascii-codec-cant-encode-characters-in-position-0-6-ordin">UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode characters in position 0-6: ordinal not in range(128)</a> - Not sure if this applies, he's using a GUI, getting input, and all in Greek.</p></li>
  </ul>
  
  <p>What's causing this error? Is it the age of the system/OS, the version of Python, or some programming error?</p>
  
  <p><strong>EDIT</strong>:
  This error crops up later with this duplicate issue (just thought I'd add it as it is within the same program and is the same error):</p>
  
  <pre><code>Traceback (most recent call last):
    File "snippets-convert.py", line 353, in &lt;module&gt;
      main()
    File "snippets-convert.py", line 41, in main
      menu()
    File "snippets-convert.py", line 75, in menu
      main()
    File "snippets-convert.py", line 41, in main
      menu()
    File "snippets-convert.py", line 62, in menu
      search()
    File "snippets-convert.py", line 229, in search
      print_results(search_returned)      # Print the results for the user
    File "snippets-convert.py", line 287, in print_results
      getPath(toRead)                                             # Get the path for the snippet
    File "snippets-convert.py", line 324, in getPath
      snipXMLParse(path)
    File "snippets-convert.py", line 344, in snipXMLParse
      print (chr(164),child.text)
  UnicodeEncodeError: 'ascii' codec can't encode character '\xa4' in position 0: ordinal not in range(128)
  </code></pre>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>I went into the terminal character settings and it does in fact support that character (as you can see in this screenshot:</p>
  
  <p><img src="https://i.stack.imgur.com/VxwAJ.png" alt="enter image description here"></p>
  
  <p>when I insert it into terminal it prints out this: <code>\342\234\202</code> and when I press <kbd>Enter</kbd> I get this: <code>-bash: ✂: command not found</code></p>
  
  <p><strong>EDIT</strong> Ran commands as @J.F. Sebastian asked:</p>
  
  <p><code>python3 test-io-encoding.py</code>:</p>
  
  <pre><code>PYTHONIOENCODING:       None
  locale(False):  US-ASCII
  device(stdout): US-ASCII
  stdout.encoding:        US-ASCII
  device(stderr): US-ASCII
  stderr.encoding:        US-ASCII
  device(stdin):  US-ASCII
  stdin.encoding: US-ASCII
  locale(False):  US-ASCII
  locale(True):   US-ASCII
  </code></pre>
  
  <p><code>python3 -S test-io-encoding.py</code>:</p>
  
  <pre><code>PYTHONIOENCODING:       None
  locale(False):  US-ASCII
  device(stdout): US-ASCII
  stdout.encoding:        US-ASCII
  device(stderr): US-ASCII
  stderr.encoding:        US-ASCII
  device(stdin):  US-ASCII
  stdin.encoding: US-ASCII
  locale(False):  US-ASCII
  locale(True):   US-ASCII
  </code></pre>
  
  <p><strong>EDIT</strong> Tried the "hackerish" solution provided by @PauloBu:</p>
  
  <p>As you can see, this caused one (Yay!) scissor, but I am now getting a new error. Traceback/error:</p>
  
  <pre><code>+-=============================-+
  ✂Traceback (most recent call last):
    File "snippets-convert.py", line 357, in &lt;module&gt;
      main()
    File "snippets-convert.py", line 44, in main
      menu()
    File "snippets-convert.py", line 52, in menu
      print("|\t "+sys.stdout.buffer.write(chr(9986).encode('UTF-8'))+" PySnipt'd "+ sys.stdout.buffer.write(chr(9986).encode('UTF-8'))+" \t|")
  TypeError: Can't convert 'int' object to str implicitly
  </code></pre>
  
  <p><strong>EDIT</strong> Added results of @PauloBu's fix:</p>
  
  <pre><code>+-=============================-+
  |
  ✂ PySnipt'd 
  ✂       |
  +-=============================-+
  </code></pre>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>And his fix for his fix:</p>
  
  <pre><code>+-=============================-+
  ✂✂|       PySnipt'd     |
  +-=============================-+
  </code></pre>