Search code examples
pythonunicode

Code runs from interpreter but not in editor


I am trying to add some symbols to a text file, I can not define these symbols in the editor but it works from the command line.

symbols = '$¢£¥€¤' works in the interpreter but not the editor (Sublime Text), however, it doesn't print these symbols correctly in command. If I do decode("utf-8"), then print works fine.

symbols = '$¢£¥€¤'
s=symbols.decode("utf-8")

I use Python 2.7 and Sublime Text

this is the error I get when I run using the editor

SyntaxError: Non-ASCII character '\xc2' in file /home/programmer/Desktop/seleniumIns.py on line 184, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

How can I fix these to add them to my original program in the editor?


Solution

  • When you run a python file containing unicode, you need to tell the interpreter what encoding is used. In your case put at the very first line of your script this line:

    # -*- coding: utf-8 -*-

    And you'll be able to use utf-8!