title: How to print non-ascii characters to file in Python 2.7
body: <p>I'm trying to obfuscate some javascript by altering their character codes, but I've found that I can't correctly print characters outside of a certain range, in Python 2.7.</p>
  
  <p>For example, here's what I'm trying to do:</p>
  
  <pre><code>f = open('text.txt','w')
  f.write(unichr(510).encode('utf-8'))
  f.close()
  </code></pre>
  
  <p>I can't write unichr(510) because it says the ascii codec is out of range.  So I encode it with utf-8.  This turns a single character <code>u'\u01fe'</code> into two <code>'\xc7\xbe'</code>.</p>
  
  <p>Now, in javascript, it's easy to get the symbol for the character code 510: </p>
  
  <pre><code>String.fromCharCode(510)
  </code></pre>
  
  <p>Gives the single character: Ǿ</p>
  
  <p>What I'm getting with Python is two characters: Ç¾</p>
  
  <p>If I pass those characters to javascript, I can't retrieve the original single character.</p>
  
  <p>I know that it is possible to print the Ǿ character in python, but I haven't been able to figure it out.  I've gotten as far as using unichr() instead of chr(), and encoding it to 'utf-8', but I'm still coming up short.  I've also read that Python 3 has this functionality built-in to the chr() function.  But that won't help me.  </p>
  
  <p>Does anyone know how I can accomplish this task?</p>
  
  <p>Thank you.</p>