title: encoding special characters in python2
body: <p>I have a piece of code that works well in Python3:</p>
  
  <pre><code>def encode_test(filepath, char_to_int):
      with open(filepath, "r", encoding= "latin-1") as f:
          dat = [line.rstrip() for line in f]
          string_to_int = [[char_to_int[char] if char != 'ó' else char_to_int['ò'] for char in line] for line in dat]
  </code></pre>
  
  <p>However, when I try to do this in Python2.7, I first got the error</p>
  
  <pre><code>SyntaxError: Non-ASCII character '\xc3' in file languageIdentification.py on line 30, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
  </code></pre>
  
  <p>Then I realize that I may need to add #coding=utf-8 at the top of code. However, after doing this, I encountered another error:</p>
  
  <pre><code>UnicodeWarning: Unicode unequal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  string_to_int = [[char_to_int[char] if char != 'ó' else char_to_int['ò'] for char in line] for line in dat]
  Traceback (most recent call last):
  File "languageIdentification.py", line 190, in &lt;module&gt;
  test_string = encode_test(sys.argv[3], char_to_int)
  File "languageIdentification.py", line 32, in encode_test
  string_to_int = [[char_to_int[char] if char != 'ó' else 
  char_to_int['ò'] for char in line] for line in dat]
  KeyError: u'\xf3'
  </code></pre>
  
  <p>So could anyone tell me what could I do to solve this problem in Python2.7?</p>
  
  <p>Thank you!</p>