Aaron Ramsey
Aaron Ramsey

Reputation: 45

Python: How to solve SyntaxError: Non-ASCII character?

This is the unicode that I have defined at the top of my program

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

And yet I still get this error

SyntaxError: Non-ASCII character '\xfe' in file C:/Users/aaron/Desktop/Python/Bicycle_Diagnosis_System/Main.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

I have looked at the website it wprovides and trawled other websites and still can't find the answer. Any ideas (im using pycharm community edition as my IDE if that affects it)

Any help is much appreciated!

Upvotes: 4

Views: 15545

Answers (2)

Edwardyao
Edwardyao

Reputation: 2061

Trying add #coding=utf-8 on line 1 and re-run

Upvotes: 2

Mark Tolonen
Mark Tolonen

Reputation: 178379

You file is saved as UTF-16 with BOM (big-endian). I saved your sample code in Notepad++ with that encoding and reproduced the error:

  File "test.py", line 1
SyntaxError: Non-ASCII character '\xfe' in file x.py on line 1, but no encoding declared; see http://python.org/dev/peps
/pep-0263/ for details

Make sure your file is saved in the encoding declared. You have to use an encoding compatible with ASCII for the hash bang and encoding lines to be read properly. UTF-16 is not compatible, hence the error message when it read the non-ASCII bytes of the byte order mark (BOM) character.

Upvotes: 0

Related Questions