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

ID : 10322

viewed : 92

Tags : pythonunicodepython-unicodecharacter-encodingpython

Top 5 Answer for python - SyntaxError: Non-ASCII character 'xa3' in file when function returns '£'

vote vote

96

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.

vote vote

86

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 -*-  
vote vote

70

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 
vote vote

68

The error message tells you exactly what's wrong. The Python interpreter needs to know the encoding of the non-ASCII character.

If you want to return U+00A3 then you can say

return u'\u00a3' 

which represents this character in pure ASCII by way of a Unicode escape sequence. If you want to return a byte string containing the literal byte 0xA3, that's

return b'\xa3' 

(where in Python 2 the b is implicit; but explicit is better than implicit).

The linked PEP in the error message instructs you exactly how to tell Python "this file is not pure ASCII; here's the encoding I'm using". If the encoding is UTF-8, that would be

# coding=utf-8 

or the Emacs-compatible

# -*- encoding: utf-8 -*- 

If you don't know which encoding your editor uses to save this file, examine it with something like a hex editor and some googling. The Stack Overflow tag has a tag info page with more information and some troubleshooting tips.

In so many words, outside of the 7-bit ASCII range (0x00-0x7F), Python can't and mustn't guess what string a sequence of bytes represents. https://tripleee.github.io/8bit#a3 shows 21 possible interpretations for the byte 0xA3 and that's only from the legacy 8-bit encodings; but it could also very well be the first byte of a multi-byte encoding. But in fact, I would guess you are actually using Latin-1, so you should have

# coding: latin-1 

as the first or second line of your source file. Anyway, without knowledge of which character the byte is supposed to represent, a human would not be able to guess this, either.

A caveat: coding: latin-1 will definitely remove the error message (because there are no byte sequences which are not technically permitted in this encoding), but might produce completely the wrong result when the code is interpreted if the actual encoding is something else. You really have to know the encoding of the file with complete certainty when you declare the encoding.

vote vote

60

Adding the following two lines in the script solved the issue for me.

# !/usr/bin/python # coding=utf-8 

Hope it helps !

Top 3 video Explaining python - SyntaxError: Non-ASCII character 'xa3' in file when function returns '£'

Related QUESTION?