SyntaxError: Non-ASCII character '\xa3' in file when function returns '£' SyntaxError: Non-ASCII character '\xa3' in file when function returns '£' python python

SyntaxError: Non-ASCII character '\xa3' in file when function returns '£'


I'd recommend reading that PEP the error gives you. The problem is that your code is trying to use the ASCII encoding, but the pound symbol is not an ASCII character. Try using UTF-8 encoding. You can start by putting # -*- coding: utf-8 -*- at the top of your .py file. To get more advanced, you can also define encodings on a string by string basis in your code. However, if you are trying to put the pound sign literal in to your code, you'll need an encoding that supports it for the entire file.


Adding the following two lines at the top of my .py script worked for me (first line was necessary):

#!/usr/bin/env python# -*- coding: utf-8 -*- 


First add the # -*- coding: utf-8 -*- line to the beginning of the file and then use u'foo' for all your non-ASCII unicode data:

def NewFunction():    return u'£'

or use the magic available since Python 2.6 to make it automatic:

from __future__ import unicode_literals


matomo