1

Although I included

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

in the first line of the python file, I keep getting

SyntaxError: Non-ASCII character '\xfe' in file C:\Users\user\PycharmProjects\my_project\my_script.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

I use PyCharm Community Edition on Windows 7. Please help.

2
  • Try reload(sys);sys.setdefaultencoding('utf8') at the start. (It is the same as shebang) Commented Jan 10, 2015 at 19:44
  • A hex dump of the first line of the file would be a helpful addition to your question. Commented Jan 10, 2015 at 20:00

3 Answers 3

5

Your file is not saved as UTF-8. Most probably you have saved in some legacy 8-bit encoding (ISO-8859-1 or the like).

Sign up to request clarification or add additional context in comments.

2 Comments

UTF-16 is also a realistic possibility. In fact, a UTF-16le BOM would cause exactly this symptom.
A UTF-16LE BOM wouldn't cause this. That would error out on b'\xff'. It would have to be UTF-16BE, which is far less likely. Your answer itself is likely correct.
0

I am not sure if I totally understood your question.

But sometimes this can solve the issue:

import sys
reload(sys)
setdefaultencoding('utf-8') 

Or try to open the file by applied an encoding format. for instance:

import codecs
 with codecs.open(filename, 'r3', encoding='utf-8') as fd:
     do something.

Comments

0

It file is with encoding wrong. Change it to UTF-8 and try again.

Example in notepad ++:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.