Python error collection: "no encoding declared" workaround

You will encounter this problem when you are just starting to learn crawling.

C:\Users\ME\Anaconda2\python.exe "Basic usage of C:/Users/ME/Desktop/Python project/crawler/request.py"
  File "C:/Users/ME/Desktop/Python project/����/request�Ļ����÷�.py", line 8
SyntaxError: Non-ASCII character '\xe8' in file C:/Users/ME/Desktop/Python project/����/request�Ļ����÷�.py on line 8, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

After reading the code for a long time, I found that there were no errors. Later, I translated it and realized that there was no coding statement.

The environment I use is python2, if you are python3 you can pass.

I often like comments when writing code, and I use Chinese

#params This parameter is used to construct the link

If you want to write Chinese in the py file of python2, you must add a line of comments declaring the file encoding, otherwise python2 will use ASCII encoding by default.

So we have to convert the encoding in the first line, the first line, it must be the first line . Because python is an interpreted language, code is interpreted from the top down.

you can do this

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

can also be

#coding=utf-8  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325588421&siteId=291194637