My first step in Python

I'm trying to start learning Python, but I was embarrassed from the first step. I start with Hello, World , but when I try to run the script, I get:

Syntax error: non-UTF-8 code starting with ' \xe9 ' in file C:\Documents and Settings\Home\workspace\Yassine frist stared\src\firstModule.py on line 5, but no encoding declared; See http://python.org/dev/peps/pep-0263/ for more details.

+4
source share
5 answers

add to first line

 # -*- coding: utf-8 -*- 
+11
source

Place the following as the first line of your program:

 # coding: utf-8 

See also The correct way to determine the encoding of Python source code.

+4
source

First of all, you should know what encoding is. Read the Absolute Minimum Every software developer should absolutely, be positively aware of Unicode and character sets (no excuses!) .

Now the problem is that most people write code in ASCII. Roughly speaking, this means that they use Latin letters, numbers and basic punctuation only in the code files themselves. It seems you used non-ASCII character code inside your program, which confuses Python.

There are two ways to fix this. Firstly, you need to tell Python what encoding you would like it to read the text file. You can do this by adding the # coding declaration at the top of the tile. The second and probably better is to limit yourself to ASCII code. Remember that you can always have any characters that you like inside strings by writing them in their encoded form, for example. \x00 or something else.

+1
source

When you start Python through the interpreter, you must run it in this format: python filename.py (command line args), or you will also get this error. I made a comment because you mentioned that you are new.

0
source

otherwise, use the built-in encode () function to consider what type you want to use considering s = "some value" s = s.encode ("ASCII", "ignore")

-1
source

All Articles