title: how to replace non ascii char in python
body: <p>I need to replace non ASCII char like <code>¾</code> in Python but I get</p>
  
  <pre><code>SyntaxError: Non-ASCII character '\xc2' in file test.py but no encoding declared; see http://www.python.org/peps/pep-0263.html for details`
  </code></pre>
  
  <p>After following the directions <a href="http://www.python.org/peps/pep-0263.html" rel="nofollow noreferrer">on the webpage</a>, I am getting </p>
  
  <pre><code>UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 449: ordinal not in range(128)
  </code></pre>
  
  <p>Here's my code:</p>
  
  <pre><code>data = data.replace(u"½", u"1/2")
  data = re.sub(u"¾", u"3/4", data, flags=re.DOTALL)
  </code></pre>
  
  <p>What do I need to change in my code?</p>
  
  <hr>
  
  <p>my file is:</p>
  
  <pre><code>#!/usr/bin/python
  
  with codecs.open("file.txt", "r", "utf8") as myfile:
      data = myfile.read()
  
  data = data.replace(u"½", u"1/2")
  </code></pre>
  
  <p>file.txt is:</p>
  
  <pre><code>hello world ½
  </code></pre>