SyntaxError: Non-ASCII character – Python with UTF-8 encoding


My Python program produce a following error message upon execution:

 SyntaxError: Non-ASCII character '\xc4' in file on line 1, but no encoding declared; 


Normally the above error message is displayed by python when other characters other then ASCII are used withing your code. The solution is to either remove all non-ASCII characters or include the bellow line into your code to enable UTF-8 encoding:

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

This will allow you to print also non-ASCII character within your code example:

$ cat
# - *- coding: utf- 8 - *-
print "Ľuboš"
$ python

Comments and Discussions
Linux Forum